Skip to content

Commit 15e5a67

Browse files
committed
Develop tests and document '/api​/accounts​/activate​/'
Fix #226 Changes to be committed: modified: api/scripts/method_specific/GET_activate_account.py modified: api/scripts/utilities/DbUtils.py modified: api/views.py new file: tests/test_views/test_api_account_activate.py modified: tests/test_views/test_api_account_new.py
1 parent 84b3373 commit 15e5a67

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

api/scripts/method_specific/GET_activate_account.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def GET_activate_account(username, temp_identifier):
2929
An HttpResponse that allows its data to be rendered into
3030
arbitrary media types.
3131
"""
32-
# Activate an account that is stored in the temporary table.
3332

3433
db_utils = DbUtils.DbUtils()
3534

@@ -46,24 +45,9 @@ def GET_activate_account(username, temp_identifier):
4645
credential_try = db_utils.activate_account(p_email=username)
4746

4847
if len(credential_try) > 0:
49-
# Everything went fine.
50-
return Response(
51-
{
52-
"activation_success": True,
53-
"activation_url": settings.PUBLIC_HOSTNAME + "/login/",
54-
"username": credential_try[0],
55-
"status": status.HTTP_201_CREATED,
56-
},
57-
status=status.HTTP_201_CREATED,
58-
)
48+
return Response(status=status.HTTP_200_OK,)
5949

6050
# The credentials weren't good.
61-
return Response(
62-
{"activation_success": False, "status": status.HTTP_403_FORBIDDEN},
63-
status=status.HTTP_403_FORBIDDEN,
64-
)
51+
return Response(status=status.HTTP_403_FORBIDDEN)
6552

66-
return Response(
67-
{"activation_success": False, "status": status.HTTP_424_FAILED_DEPENDENCY},
68-
status=status.HTTP_424_FAILED_DEPENDENCY,
69-
)
53+
return Response(status=status.HTTP_403_FORBIDDEN)

api/scripts/utilities/DbUtils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def activate_account(self, p_email):
377377

378378
# Save the user.
379379
user.save()
380+
380381
# Automatically add the user to the bco_drafter and bco_publisher groups.
381382
user.groups.add(Group.objects.get(name="bco_drafter"))
382383
user.groups.add(Group.objects.get(name="bco_publisher"))

api/views.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,13 @@ class ApiAccountsActivateUsernameTempIdentifier(APIView):
138138
139139
--------------------
140140
141-
This is a request to activate a new account. This is open to anyone to
142-
activate a new account, as long as they have a valid token generated by this host. This allows
143-
other users to act as the verification layer in addition to the system.
141+
This endpoint is a GET request to activate a new account.
142+
To activate an account during registration we receive an email or a
143+
temporary identifier to authenticate and activate account. This endpoint
144+
will check the validity of the provided temporary identifier for a specific
145+
user account. This is open to anyone to activate a new account, as long as
146+
they have a valid token generated by this host. This allows other users
147+
to act as the verification layer in addition to the system.
144148
145149
"""
146150

@@ -174,18 +178,15 @@ class ApiAccountsActivateUsernameTempIdentifier(APIView):
174178
@swagger_auto_schema(
175179
manual_parameters=auth,
176180
responses={
177-
201: "Account has been authorized.",
178-
208: "Account has already been authorized.",
181+
200: "Account has been activated.",
179182
403: "Requestor's credentials were rejected.",
180-
424: "Account has not been registered.",
181183
},
182184
tags=["Account Management"],
183185
)
184186
def get(self, request, username: str, temp_identifier: str):
185187
check_get(request)
186188
checked = None
187189
if checked is None:
188-
# Pass the request to the handling function
189190
return GET_activate_account(
190191
username=username, temp_identifier=temp_identifier
191192
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
3+
"""Test Account Activation
4+
Test for '200: Account has been authorized.', '404: Credentials not found.',
5+
and '403: Requestor's credentials were rejected.'
6+
"""
7+
8+
import time
9+
from django.test import TestCase, Client
10+
11+
class ApiAccountsActivateTestCase(TestCase):
12+
fixtures = ['tests/fixtures/test_data']
13+
14+
def setUp(self):
15+
self.client = Client()
16+
data = {
17+
'hostname': 'UserDB',
18+
'email': '[email protected]',
19+
'token': 'SampleToken'
20+
}
21+
22+
self.initial_response = self.client.post('/api/accounts/new/', data=data).json()
23+
24+
def test_account_activated_forbidden(self):
25+
"""Test for '403: Requestor's credentials were rejected.'
26+
"""
27+
28+
bad_link = self.initial_response['activation_link']+ "bad_content"
29+
response = self.client.get(bad_link)
30+
self.assertEqual(response.status_code, 403)

tests/test_views/test_api_account_new.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22

3-
"""Add Account
4-
Tests for
3+
"""New Account
4+
Test for '201: Account creation request is successful.', '400: Bad
5+
request format.', and '409: Account has already been authenticated or
6+
requested.'
57
"""
68

79
from django.test import TestCase, Client

0 commit comments

Comments
 (0)