Skip to content

Commit b7e72f9

Browse files
authored
Update mutations getting the input object treatment (canada-ca#642)
* Refactored update domain to use input object * Update faker * Update descriptions * Refactor updateOrganization to use input object * Update descriptions * Update faker
1 parent 2a46a25 commit b7e72f9

6 files changed

Lines changed: 292 additions & 196 deletions

File tree

api/queries.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,26 @@ class Mutation(graphene.ObjectType):
180180
authenticate_two_factor = ValidateTwoFactor.Field()
181181
update_user_role = UpdateUserRole.Field()
182182
create_organization = CreateOrganization.Field(
183-
description="Allows the creation of an organization inside the " "database.",
183+
description="Allows the creation of an organization inside the database.",
184184
)
185185
update_organization = UpdateOrganization.Field(
186-
description="Allows modification of an organization inside the " "database."
186+
description="Allows modification of an organization inside the database."
187187
)
188188
remove_organization = RemoveOrganization.Field(
189-
description="Allows the removal of an organization inside the database"
189+
description="Allows the removal of an organization inside the database."
190190
)
191191
create_domain = CreateDomain.Field(
192-
description="Allows the creation of domains for a given organization"
192+
description="Allows the creation of domains for a given organization."
193+
)
194+
update_domain = UpdateDomain.Field(
195+
description="Allows the modification of a given domain."
193196
)
194-
update_domain = UpdateDomain.Field(description="Allows the modification of domains")
195197
remove_domain = RemoveDomain.Field(
196-
description="Allows the removal of a given domain"
198+
description="Allows the removal of a given domain."
197199
)
198200
request_scan = RequestScan.Field()
199201
authenticate = Authenticate.Field(
200-
description="Allows users to give their credentials and be " "authenticated"
202+
description="Allows users to give their credentials and be authenticated"
201203
)
202204
sign_up = SignUp.Field(description="Allows users to sign up to our service")
203205
email_verify_account = EmailVerifyAccount.Field(

api/schemas/update_domain/update_domain.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,51 @@
1313
from scalars.selectors import Selectors
1414

1515

16+
class UpdateDomainInput(graphene.InputObjectType):
17+
"""
18+
Input object used to define the argument fields for the updateDomain
19+
mutation.
20+
"""
21+
22+
current_url = URL(
23+
description="The current domain that is being requested to be updated.",
24+
required=True,
25+
)
26+
updated_url = URL(
27+
description="The new domain you wish to update the current domain to be.",
28+
required=True,
29+
)
30+
updated_selectors = Selectors(
31+
description="The new DKIM selector strings corresponding to this domain.",
32+
required=False,
33+
)
34+
35+
1636
class UpdateDomain(graphene.Mutation):
1737
"""
1838
Mutation allows the modification of domains if domain is updated through
1939
out its life-cycle
2040
"""
2141

2242
class Arguments:
23-
current_url = URL(
24-
description="The current domain that is being requested to be updated.",
43+
input = UpdateDomainInput(
2544
required=True,
26-
)
27-
updated_url = URL(
28-
description="The new domain you wish to update the current domain "
29-
"to be.",
30-
required=True,
31-
)
32-
updated_selectors = Selectors(
33-
description="The new DKIM selector strings corresponding to this domain",
34-
required=False,
45+
description="updateDomain input object containing all arguement fields.",
3546
)
3647

37-
status = graphene.Boolean()
48+
status = graphene.Boolean(
49+
description="Returns true if domain was successfully updated."
50+
)
3851

3952
@require_token
4053
def mutate(self, info, **kwargs):
4154
user_id = kwargs.get("user_id")
4255
user_roles = kwargs.get("user_roles")
43-
current_domain = cleanse_input(kwargs.get("current_url"))
44-
updated_domain = cleanse_input(kwargs.get("updated_url"))
45-
updated_selectors = cleanse_input_list(kwargs.get("updated_selectors", []))
56+
current_domain = cleanse_input(kwargs.get("input", {}).get("current_url"))
57+
updated_domain = cleanse_input(kwargs.get("input", {}).get("updated_url"))
58+
updated_selectors = cleanse_input_list(
59+
kwargs.get("input", {}).get("updated_selectors", [])
60+
)
4661

4762
# Check to see if current domain exists
4863
domain_orm = Domains.query.filter(Domains.domain == current_domain).first()

api/schemas/update_organization/update_organization.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,61 @@
1313
from scalars.slug import Slug
1414

1515

16+
class UpdateOrganizationInput(graphene.InputObjectType):
17+
"""
18+
Input object used to define the argument fields for the updateOrganization
19+
mutation.
20+
"""
21+
22+
slug = Slug(description="Organization that will be updated.", required=True)
23+
acronym = Acronym(
24+
description="Organization Acronym you would like updated.", required=False
25+
)
26+
name = graphene.String(description="Full name of organization.", required=False)
27+
zone = graphene.String(
28+
description="The zone which the organization belongs to.", required=False
29+
)
30+
sector = graphene.String(
31+
description="The sector which the organization belongs to.", required=False
32+
)
33+
province = graphene.String(
34+
description="The province in which the organization is located in.",
35+
required=False,
36+
)
37+
city = graphene.String(
38+
description="The city in which the organization is located in.", required=False,
39+
)
40+
41+
1642
class UpdateOrganization(graphene.Mutation):
43+
"""
44+
Mutation allows the modification of organizations if any changes to the
45+
organization may occur.
46+
"""
47+
1748
class Arguments:
18-
slug = Slug(description="Organization that will be updated", required=True)
19-
acronym = Acronym(
20-
description="Organization Acronym you would like updated", required=False
21-
)
22-
name = graphene.String(description="Full name of organization.", required=False)
23-
zone = graphene.String(
24-
description="The zone which the organization belongs to.", required=False
25-
)
26-
sector = graphene.String(
27-
description="The sector which the organization belongs to.", required=False
28-
)
29-
province = graphene.String(
30-
description="The province in which the organization is located in.",
31-
required=False,
32-
)
33-
city = graphene.String(
34-
description="The city in which the organization is located in.",
35-
required=False,
49+
input = UpdateOrganizationInput(
50+
required=True,
51+
description="UpdateOrganizationInput object containing all arguement fields.",
3652
)
3753

3854
# If the update passed or failed
39-
status = graphene.Boolean()
55+
status = graphene.Boolean(
56+
description="Returns true if organization was successfully updated."
57+
)
4058

4159
@require_token
4260
def mutate(self, info, **kwargs):
4361
# Get arguments from mutation
4462
user_id = kwargs.get("user_id")
4563
user_roles = kwargs.get("user_roles")
46-
slug = cleanse_input(kwargs.get("slug"))
47-
name = cleanse_input(kwargs.get("name"))
48-
acronym = cleanse_input(kwargs.get("acronym"))
49-
zone = cleanse_input(kwargs.get("zone"))
50-
sector = cleanse_input(kwargs.get("sector"))
51-
province = cleanse_input(kwargs.get("province"))
52-
city = cleanse_input(kwargs.get("city"))
64+
slug = cleanse_input(kwargs.get("input", {}).get("slug"))
65+
name = cleanse_input(kwargs.get("input", {}).get("name"))
66+
acronym = cleanse_input(kwargs.get("input", {}).get("acronym"))
67+
zone = cleanse_input(kwargs.get("input", {}).get("zone"))
68+
sector = cleanse_input(kwargs.get("input", {}).get("sector"))
69+
province = cleanse_input(kwargs.get("input", {}).get("province"))
70+
city = cleanse_input(kwargs.get("input", {}).get("city"))
5371

5472
# XXX: only the Super User can edit orgs?
5573
if is_super_admin(user_roles=user_roles):

api/tests/test_updateDomain_mutation.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ def test_domain_update_super_admin(save, caplog):
5555
mutation="""
5656
mutation{
5757
updateDomain(
58-
currentUrl: "sa.update.domain.ca",
59-
updatedUrl: "updated.sa.update.domain.ca"
58+
input: {
59+
currentUrl: "sa.update.domain.ca",
60+
updatedUrl: "updated.sa.update.domain.ca"
61+
}
6062
) {
6163
status
6264
}
@@ -121,8 +123,10 @@ def test_domain_update_super_admin_cant_update_domain_doesnt_exist(save, caplog)
121123
mutation="""
122124
mutation{
123125
updateDomain(
124-
currentUrl: "sa.update.domain.ca",
125-
updatedUrl: "updated.sa.update.domain.ca"
126+
input: {
127+
currentUrl: "sa.update.domain.ca",
128+
updatedUrl: "updated.sa.update.domain.ca"
129+
}
126130
) {
127131
status
128132
}
@@ -171,8 +175,10 @@ def test_domain_update_org_admin(save, caplog):
171175
mutation="""
172176
mutation{
173177
updateDomain(
174-
currentUrl: "admin.update.domain.ca",
175-
updatedUrl: "updated.admin.create.domain.ca"
178+
input: {
179+
currentUrl: "admin.update.domain.ca",
180+
updatedUrl: "updated.admin.create.domain.ca"
181+
}
176182
) {
177183
status
178184
}
@@ -243,8 +249,10 @@ def test_domain_update_org_admin_cant_update_in_different_org(save, caplog):
243249
mutation="""
244250
mutation{
245251
updateDomain(
246-
currentUrl: "admin2.update.domain.ca",
247-
updatedUrl: "updated.admin2.update.domain.ca"
252+
input: {
253+
currentUrl: "admin2.update.domain.ca",
254+
updatedUrl: "updated.admin2.update.domain.ca"
255+
}
248256
) {
249257
status
250258
}
@@ -291,8 +299,10 @@ def test_domain_update_org_admin_cant_update_domain_doesnt_exist(save, caplog):
291299
mutation="""
292300
mutation{
293301
updateDomain(
294-
currentUrl: "admin2.update.domain.ca",
295-
updatedUrl: "updated.admin2.update.domain.ca"
302+
input: {
303+
currentUrl: "admin2.update.domain.ca",
304+
updatedUrl: "updated.admin2.update.domain.ca"
305+
}
296306
) {
297307
status
298308
}
@@ -341,8 +351,10 @@ def test_domain_update_user_write(save, caplog):
341351
mutation="""
342352
mutation{
343353
updateDomain(
344-
currentUrl: "user.write.domain.ca",
345-
updatedUrl: "updated.user.write.domain.ca"
354+
input: {
355+
currentUrl: "user.write.domain.ca",
356+
updatedUrl: "updated.user.write.domain.ca"
357+
}
346358
) {
347359
status
348360
}
@@ -414,8 +426,10 @@ def test_domain_update_user_write_cant_update_in_different_org(save, caplog):
414426
mutation="""
415427
mutation{
416428
updateDomain(
417-
currentUrl: "user.write.domain.ca",
418-
updatedUrl: "updated.user.write.domain.ca"
429+
input: {
430+
currentUrl: "user.write.domain.ca",
431+
updatedUrl: "updated.user.write.domain.ca"
432+
}
419433
) {
420434
status
421435
}
@@ -462,8 +476,10 @@ def test_domain_update_user_write_cant_update_domain_doesnt_exist(save, caplog):
462476
mutation="""
463477
mutation{
464478
updateDomain(
465-
currentUrl: "user.write.domain.ca",
466-
updatedUrl: "updated.user.write.domain.ca"
479+
input: {
480+
currentUrl: "user.write.domain.ca",
481+
updatedUrl: "updated.user.write.domain.ca"
482+
}
467483
) {
468484
status
469485
}
@@ -512,8 +528,10 @@ def test_domain_update_user_read_cant_update_domain(save, caplog):
512528
mutation="""
513529
mutation{
514530
updateDomain(
515-
currentUrl: "user.read.domain.ca",
516-
updatedUrl: "updated.user.read.domain.ca"
531+
input: {
532+
currentUrl: "user.read.domain.ca",
533+
updatedUrl: "updated.user.read.domain.ca"
534+
}
517535
) {
518536
status
519537
}

0 commit comments

Comments
 (0)