Skip to content

Commit 2530f7d

Browse files
committed
Cleaning up some things
Changes to be committed: modified: biocompute/apis.py modified: biocompute/migrations/0001_initial.py modified: prefix/apis.py modified: prefix/migrations/0001_initial.py new file: tests/test_views/test_account_activate.py new file: tests/test_views/test_account_describe.py new file: tests/test_views/test_account_new.py new file: tests/test_views/test_auth_add.py new file: tests/test_views/test_auth_remove.py new file: tests/test_views/test_auth_reset_token.py new file: tests/test_views/test_objects_drafts_create.py
1 parent 1b20196 commit 2530f7d

File tree

11 files changed

+449
-17
lines changed

11 files changed

+449
-17
lines changed

biocompute/apis.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@
1515
from config.services import legacy_api_converter, response_constructor
1616
from biocompute.services import BcoDraftSerializer
1717

18-
class DraftsCreateApi(APIView):
19-
"""
20-
Create BCO Draft [Bulk Enabled]
21-
22-
--------------------
23-
24-
Creates a new BCO draft object.
25-
"""
26-
27-
request_body = openapi.Schema(
18+
BCO_DRAFT_SCHEMA = openapi.Schema(
2819
type=openapi.TYPE_ARRAY,
2920
title="Create BCO Draft Schema",
3021
items=openapi.Schema(
@@ -61,6 +52,17 @@ class DraftsCreateApi(APIView):
6152
description="BCO Drafts to create.",
6253
)
6354

55+
class DraftsCreateApi(APIView):
56+
"""
57+
Create BCO Draft [Bulk Enabled]
58+
59+
--------------------
60+
61+
Creates a new BCO draft object.
62+
"""
63+
64+
request_body = BCO_DRAFT_SCHEMA
65+
6466
@swagger_auto_schema(
6567
request_body=request_body,
6668
responses={

biocompute/migrations/0001_initial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.13 on 2024-03-14 13:52
1+
# Generated by Django 3.2.13 on 2024-03-20 18:48
22

33
from django.conf import settings
44
from django.db import migrations, models
@@ -10,8 +10,8 @@ class Migration(migrations.Migration):
1010
initial = True
1111

1212
dependencies = [
13-
('auth', '0012_alter_user_first_name_max_length'),
1413
('prefix', '0001_initial'),
14+
('auth', '0012_alter_user_first_name_max_length'),
1515
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
1616
]
1717

prefix/apis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
class PrefixesCreateApi(APIView):
4040
"""
41-
Create a Prefix
41+
Create a Prefix [Bulk Enabled]
4242
4343
--------------------
4444
Create a prefix to be used to classify BCOs and to determine permissions
@@ -119,7 +119,7 @@ def post(self, request) -> Response:
119119

120120
class PrefixesDeleteApi(APIView):
121121
"""
122-
Delete a Prefix
122+
Delete a Prefix [Bulk Enabled]
123123
124124
# Deletes a prefix for BCOs.
125125
--------------------
@@ -209,7 +209,7 @@ def post(self, request) -> Response:
209209

210210
class PrefixesModifyApi(APIView):
211211
"""
212-
Modify a Prefix
212+
Modify a Prefix [Bulk Enabled]
213213
214214
--------------------
215215

prefix/migrations/0001_initial.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.13 on 2024-03-14 13:14
1+
# Generated by Django 3.2.13 on 2024-03-20 18:48
22

33
from django.conf import settings
44
from django.db import migrations, models
@@ -11,8 +11,8 @@ class Migration(migrations.Migration):
1111
initial = True
1212

1313
dependencies = [
14-
('auth', '0012_alter_user_first_name_max_length'),
1514
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
15+
('auth', '0012_alter_user_first_name_max_length'),
1616
]
1717

1818
operations = [
@@ -23,6 +23,7 @@ class Migration(migrations.Migration):
2323
('certifying_key', models.TextField(blank=True, null=True)),
2424
('created', models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True)),
2525
('description', models.TextField(blank=True, null=True)),
26+
('counter', models.IntegerField(default=0, help_text='Counter for object_id asignment')),
2627
('authorized_groups', models.ManyToManyField(blank=True, related_name='authorized_prefix', to='auth.Group')),
2728
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username')),
2829
],
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
17+
def test_account_activated_success(self):
18+
"""Test for '201: Account creation request is successful.'
19+
"""
20+
21+
response = self.client.get(
22+
'/api/accounts/activate/'\
23+
+'test_new_user%40testing.com/sample_temp_identifier'
24+
)
25+
self.assertEqual(response.status_code, 200)
26+
27+
def test_account_activated_forbidden(self):
28+
"""Test for '403: Requestor's credentials were rejected.'
29+
"""
30+
31+
bad_link = "test_new_user%40testing.com/bad_temp_identifier"
32+
response = self.client.get(f'/api/accounts/activate/{bad_link}')
33+
self.assertEqual(response.status_code, 403)
34+
35+
def test_account_activated_not_found(self):
36+
"""Test for '404: That account, {email}, was not found'
37+
"""
38+
39+
bad_link = "test22%40testing.com/sample_temp_identifier"
40+
response = self.client.get(f'/api/accounts/activate/{bad_link}')
41+
self.assertEqual(response.status_code, 404)
42+
43+
def test_account_activated_conflict(self):
44+
"""Test for '409: CONFLICT: That account, {email},
45+
has already been activated.'
46+
"""
47+
48+
bad_link = "tester%40testing.com/sample_temp_identifier"
49+
response = self.client.get(f'/api/accounts/activate/{bad_link}')
50+
self.assertEqual(response.status_code, 409)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
3+
"""API- Accounts describe
4+
Tests for 'Authorization is successfull' (200),
5+
'Forbidden. Authentication credentials were not provided' (403),
6+
'Invalid Token' (403)
7+
"""
8+
9+
10+
from django.test import TestCase
11+
from django.contrib.auth.models import User
12+
from rest_framework.authtoken.models import Token
13+
from rest_framework.test import APIClient
14+
15+
class AccountDescribeTestCase(TestCase):
16+
fixtures = ['tests/fixtures/test_data']
17+
18+
def test_success_response(self):
19+
"""200: Authorization is successful.
20+
"""
21+
client = APIClient()
22+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
23+
client.credentials(HTTP_AUTHORIZATION='Token ' + token)
24+
response = client.post('/api/accounts/describe/', format='json')
25+
self.assertEqual(response.status_code, 200)
26+
27+
def test_forbidden_response(self):
28+
"""403: Forbidden. Authentication credentials were not provided.
29+
"""
30+
client = APIClient()
31+
response = client.post('/api/accounts/describe/')
32+
self.assertEqual(response.status_code, 403)
33+
34+
def test_unauthorized_response(self):
35+
"""403: Invalid token
36+
"""
37+
client = APIClient()
38+
client.credentials(HTTP_AUTHORIZATION='Token This-token-is-bad')
39+
response = client.post('/api/accounts/describe/')
40+
self.assertEqual(response.status_code, 403)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
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.'
7+
"""
8+
9+
from django.test import TestCase, Client
10+
11+
class ApiAccountsNewTestCase(TestCase):
12+
fixtures = ['tests/fixtures/test_data']
13+
14+
def setUp(self):
15+
self.client = Client()
16+
17+
def test_creation_request_success(self):
18+
""" Test for '201: Account creation request is successful.'
19+
"""
20+
21+
data = {
22+
'hostname': 'http://localhost:8000',
23+
'email': '[email protected]',
24+
'token': 'SampleToken'
25+
}
26+
27+
28+
response = self.client.post('/api/accounts/new/', data=data)
29+
self.assertEqual(response.status_code, 201)
30+
31+
def test_creation_request_success_bad_request(self):
32+
"""Test for '400: Bad request format.'
33+
"""
34+
data = {
35+
'hostname': 'UserDB',
36+
'email': '[email protected]'
37+
}
38+
39+
response = self.client.post('/api/accounts/new/', data=data)
40+
self.assertEqual(response.status_code, 400)
41+
42+
def test_creation_request_conflict(self):
43+
""" Test for '409: Account has already been authenticated or
44+
requested.'
45+
"""
46+
47+
data = {
48+
'hostname': 'http://localhost:8000',
49+
'email': '[email protected]',
50+
'token': 'SampleToken'
51+
}
52+
53+
54+
response = self.client.post('/api/accounts/new/', data=data)
55+
response2 = self.client.post('/api/accounts/new/', data=data)
56+
self.assertEqual(response.status_code, 201)
57+
self.assertEqual(response2.status_code, 409)

tests/test_views/test_auth_add.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
3+
"""Add Authentication
4+
Tests for 'New authentication credentials added to existing object' (200),
5+
'Authentication credentials were created and added' (201), 'Bad request' (400),
6+
'That object already exists for this account' (409)
7+
"""
8+
9+
from django.test import TestCase, Client
10+
from rest_framework.test import APIClient
11+
from rest_framework.authtoken.models import Token
12+
from django.contrib.auth.models import User
13+
from authentication.models import Authentication
14+
15+
class AuthenticationTestCase(TestCase):
16+
fixtures = ['tests/fixtures/test_data']
17+
18+
def setUp(self):
19+
self.client = APIClient()
20+
21+
def test_credentials_created_response(self):
22+
"""Add authentication is successful (200)
23+
"""
24+
25+
token = Token.objects.get(user=User.objects.get(username='tester')).key
26+
data = {"iss": "Reeya1","sub": "ReeyaGupta1"}
27+
28+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
29+
response = self.client.post('/api/auth/add/', data=data)
30+
self.assertEqual(response.status_code, 201)
31+
32+
def test_credentials_added(self):
33+
"""New authentication credentials added to existing object (200)
34+
"""
35+
36+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
37+
data = {"iss": "new","sub": "new One"}
38+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
39+
response = self.client.post('/api/auth/add/', data=data, format='json')
40+
self.assertEqual(response.status_code, 200)
41+
42+
def test_bad_request_response(self):
43+
"""Bad request (400)
44+
"""
45+
46+
token = Token.objects.get(user=User.objects.get(username='tester')).key
47+
data = {"Missing required fields"}
48+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
49+
response = self.client.post('/api/auth/add/', data=data, format='json')
50+
self.assertEqual(response.status_code, 400)
51+
52+
def test_object_already_exists_response(self):
53+
"""That object already exists for this account (409)
54+
"""
55+
56+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
57+
data = {"iss": "Reeya1","sub": "ReeyaGupta1"}
58+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
59+
response = self.client.post('/api/auth/add/', data=data, format='json')
60+
self.assertEqual(response.status_code, 409)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
"""Remove Authentication
4+
Tests for 'Remove authentication is successful.` (200), 'Authentication
5+
failed.' (403), and 'That object does not exist for this account.' (404)
6+
"""
7+
8+
from django.test import TestCase
9+
from rest_framework.test import APIClient
10+
from rest_framework.authtoken.models import Token
11+
from django.contrib.auth.models import User
12+
from rest_framework.test import APITestCase
13+
14+
class AuthenticationRemovetestcase(APITestCase):
15+
fixtures = ['tests/fixtures/test_data']
16+
17+
def setUp(self):
18+
self.client = APIClient()
19+
20+
def test_success_response(self):
21+
"""Remove authentication is successful. (200)
22+
"""
23+
24+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
25+
26+
data = {"iss": "Reeya1","sub": "ReeyaGupta1"}
27+
28+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
29+
response = self.client.post('/api/auth/remove/', data=data, format='json')
30+
self.assertEqual(response.status_code, 200)
31+
32+
def test_bad_authentication(self):
33+
"""Authentication failed. 403
34+
"""
35+
36+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
37+
data = {}
38+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
39+
response = self.client.post('/api/auth/remove/', data=data)
40+
self.assertEqual(response.status_code, 403)
41+
42+
def test_object_already_exists_response(self):
43+
"""That object does not exist for this account. 404
44+
"""
45+
46+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
47+
data = {"iss": "Reeya2","sub": "ReeyaGupta2"}
48+
49+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
50+
response = self.client.post('/api/auth/remove/', data=data)
51+
self.assertEqual(response.status_code, 404)

0 commit comments

Comments
 (0)