Skip to content

Commit 2811005

Browse files
authored
Merge branch 'model_refactor' into bco_perms
2 parents 11827e6 + 5a4a5c9 commit 2811005

11 files changed

+870
-7
lines changed

tests/fixtures/test_data.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@
478478
},
479479
{
480480
"model": "auth.permission",
481-
"pk": 54,
481+
"pk": 67,
482482
"fields": {
483483
"name": "Can add BCOs with prefix NOPUB",
484484
"content_type": 13,
@@ -487,7 +487,7 @@
487487
},
488488
{
489489
"model": "auth.permission",
490-
"pk": 55,
490+
"pk": 68,
491491
"fields": {
492492
"name": "Can change BCOs with prefix NOPUB",
493493
"content_type": 13,
@@ -496,7 +496,7 @@
496496
},
497497
{
498498
"model": "auth.permission",
499-
"pk": 56,
499+
"pk": 69,
500500
"fields": {
501501
"name": "Can delete BCOs with prefix NOPUB",
502502
"content_type": 13,
@@ -505,7 +505,7 @@
505505
},
506506
{
507507
"model": "auth.permission",
508-
"pk": 57,
508+
"pk": 70,
509509
"fields": {
510510
"name": "Can publish BCOs with prefix NOPUB",
511511
"content_type": 13,
@@ -514,7 +514,7 @@
514514
},
515515
{
516516
"model": "auth.permission",
517-
"pk": 58,
517+
"pk": 71,
518518
"fields": {
519519
"name": "Can add new user",
520520
"content_type": 11,
@@ -523,7 +523,7 @@
523523
},
524524
{
525525
"model": "auth.permission",
526-
"pk": 59,
526+
"pk": 72,
527527
"fields": {
528528
"name": "Can change new user",
529529
"content_type": 11,
@@ -532,7 +532,7 @@
532532
},
533533
{
534534
"model": "auth.permission",
535-
"pk": 60,
535+
"pk": 73,
536536
"fields": {
537537
"name": "Can delete new user",
538538
"content_type": 11,
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)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
"""Reset Token
4+
Tests for 'Token reset is successful.' 200, and 'Bad request.', 400.
5+
"""
6+
7+
from django.test import TestCase, Client
8+
from rest_framework.test import APIClient
9+
from rest_framework.authtoken.models import Token
10+
from django.contrib.auth.models import User
11+
12+
class ResetTokenTestCase(TestCase):
13+
fixtures = ['tests/fixtures/test_data']
14+
15+
def setUp(self) -> None:
16+
self.client = APIClient()
17+
18+
def test_reset_successful(self):
19+
"""Token reset is successful. 200
20+
"""
21+
22+
token = Token.objects.get(user=User.objects.get(username='tester')).key
23+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
24+
response = self.client.post('/api/auth/reset_token/')
25+
self.assertEqual(response.status_code, 200)
26+
27+
def test_invalid_token(self):
28+
"""Invalid token. 403
29+
"""
30+
31+
token = 'this-is-an-invalid-token'
32+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
33+
response = self.client.post('/api/auth/reset_token/')
34+
self.assertEqual(response.status_code, 403)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
#!/usr/bin/env python3
3+
4+
"""Objects/Drafts_create
5+
Tests for 'Creation of BCO draft is successful.' (200),
6+
returns 207, 403 (needs to be reviewed)
7+
"""
8+
9+
10+
import json
11+
from django.test import TestCase
12+
from django.contrib.auth.models import User
13+
from rest_framework.authtoken.models import Token
14+
from rest_framework.test import APIClient
15+
16+
class BcoDraftCreateTestCase(TestCase):
17+
fixtures = ['tests/fixtures/test_data']
18+
def setUp(self):
19+
self.client = APIClient()
20+
21+
self.token = Token.objects.get(user=User.objects.get(username="tester"))
22+
23+
self.legacy_data = {
24+
"POST_api_objects_draft_create": [
25+
{
26+
"prefix": "BCO",
27+
"owner_group": "tester",
28+
"object_id": "http://127.0.0.1:8000/BCO_000002/DRAFT",
29+
"schema": "IEEE",
30+
"contents": {
31+
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT",
32+
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
33+
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
34+
}
35+
}
36+
]
37+
}
38+
39+
self.data = [
40+
{
41+
"object_id": "http://127.0.0.1:8000/BCO_000001/DRAFT",
42+
"prefix": "BCO",
43+
"authorized_users": ["hivelab"],
44+
"contents": {
45+
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT",
46+
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
47+
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
48+
}
49+
},
50+
{
51+
"object_id": "http://127.0.0.1:8000/TEST_000001",
52+
"prefix": "TEST",
53+
"contents": {
54+
"object_id": "https://biocomputeobject.org/TEST_000001",
55+
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
56+
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
57+
}
58+
}
59+
]
60+
61+
def test_legacy_successful_creation(self):
62+
"""200: Creation of BCO drafts is successful.
63+
"""
64+
65+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
66+
response = self.client.post('/api/objects/drafts/create/', self.legacy_data, format='json')
67+
self.assertEqual(response.status_code, 200)
68+
69+
def test_successful_creation(self):
70+
"""200: Creation of BCO drafts is successful.
71+
"""
72+
73+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
74+
response = self.client.post('/api/objects/drafts/create/', self.data, format='json')
75+
self.assertEqual(response.status_code, 200)
76+
77+
def test_partial_failure(self):
78+
# Test case for partial failure (response code 300)
79+
##Returns 207(Multi status) instead of 300(Partial faliure)
80+
data = {
81+
'POST_api_objects_draft_create': [
82+
{
83+
'prefix': 'BCO',
84+
'owner_group': 'bco_drafter',
85+
'schema': 'IEEE',
86+
'contents': {}
87+
},
88+
{
89+
'prefix': 'Reeyaa',
90+
'owner_group': 'bco_drafter',
91+
'schema': 'IEEE',
92+
'contents': {}
93+
}
94+
]
95+
}
96+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
97+
response = self.client.post('/api/objects/drafts/create/', data=data, format='json')
98+
self.assertEqual(response.status_code, 207)
99+
100+
def test_bad_request(self):
101+
# Test case for bad request (response code 400)
102+
#Gives 403 forbidden request instead of 400
103+
data = [
104+
{
105+
"object_id": "http://127.0.0.1:8000/TEST_000001",
106+
"prefix": "TEST",
107+
"contents": {
108+
"object_id": "https://biocomputeobject.org/TEST_000001",
109+
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
110+
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
111+
}
112+
}
113+
]
114+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
115+
response = self.client.post('/api/objects/drafts/create/', data=data, format='json')
116+
self.assertEqual(response.status_code, 400)
117+
118+
def test_invalid_token(self):
119+
# Test case for invalid token (response code 403)
120+
# Setting authentication token to an invalid value
121+
122+
data = {
123+
'POST_api_objects_draft_create': [
124+
{
125+
'prefix': 'BCO',
126+
'owner_group': 'bco_drafter',
127+
'schema': 'IEEE',
128+
'contents': {}
129+
},
130+
131+
]
132+
}
133+
self.client.credentials(HTTP_AUTHORIZATION='Token InvalidToken')
134+
response = self.client.post('/api/objects/drafts/create/', data=data, format='json')
135+
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)