-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'model_refactor' into bco_perms
- Loading branch information
Showing
11 changed files
with
870 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/test_apis/test_api_authentication/test_api_auth_add.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""Add Authentication | ||
Tests for 'New authentication credentials added to existing object' (200), | ||
'Authentication credentials were created and added' (201), 'Bad request' (400), | ||
'That object already exists for this account' (409) | ||
""" | ||
|
||
from django.test import TestCase, Client | ||
from rest_framework.test import APIClient | ||
from rest_framework.authtoken.models import Token | ||
from django.contrib.auth.models import User | ||
from authentication.models import Authentication | ||
|
||
class AuthenticationTestCase(TestCase): | ||
fixtures = ['tests/fixtures/test_data'] | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
|
||
def test_credentials_created_response(self): | ||
"""Add authentication is successful (200) | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='tester')).key | ||
data = {"iss": "Reeya1","sub": "ReeyaGupta1"} | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/add/', data=data) | ||
self.assertEqual(response.status_code, 201) | ||
|
||
def test_credentials_added(self): | ||
"""New authentication credentials added to existing object (200) | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key | ||
data = {"iss": "new","sub": "new One"} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/add/', data=data, format='json') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_bad_request_response(self): | ||
"""Bad request (400) | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='tester')).key | ||
data = {"Missing required fields"} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/add/', data=data, format='json') | ||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_object_already_exists_response(self): | ||
"""That object already exists for this account (409) | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key | ||
data = {"iss": "Reeya1","sub": "ReeyaGupta1"} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/add/', data=data, format='json') | ||
self.assertEqual(response.status_code, 409) |
51 changes: 51 additions & 0 deletions
51
tests/test_apis/test_api_authentication/test_api_auth_remove.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""Remove Authentication | ||
Tests for 'Remove authentication is successful.` (200), 'Authentication | ||
failed.' (403), and 'That object does not exist for this account.' (404) | ||
""" | ||
|
||
from django.test import TestCase | ||
from rest_framework.test import APIClient | ||
from rest_framework.authtoken.models import Token | ||
from django.contrib.auth.models import User | ||
from rest_framework.test import APITestCase | ||
|
||
class AuthenticationRemovetestcase(APITestCase): | ||
fixtures = ['tests/fixtures/test_data'] | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
|
||
def test_success_response(self): | ||
"""Remove authentication is successful. (200) | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key | ||
|
||
data = {"iss": "Reeya1","sub": "ReeyaGupta1"} | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/remove/', data=data, format='json') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_bad_authentication(self): | ||
"""Authentication failed. 403 | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key | ||
data = {} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/remove/', data=data) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def test_object_already_exists_response(self): | ||
"""That object does not exist for this account. 404 | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key | ||
data = {"iss": "Reeya2","sub": "ReeyaGupta2"} | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/remove/', data=data) | ||
self.assertEqual(response.status_code, 404) |
34 changes: 34 additions & 0 deletions
34
tests/test_apis/test_api_authentication/test_api_auth_reset_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""Reset Token | ||
Tests for 'Token reset is successful.' 200, and 'Bad request.', 400. | ||
""" | ||
|
||
from django.test import TestCase, Client | ||
from rest_framework.test import APIClient | ||
from rest_framework.authtoken.models import Token | ||
from django.contrib.auth.models import User | ||
|
||
class ResetTokenTestCase(TestCase): | ||
fixtures = ['tests/fixtures/test_data'] | ||
|
||
def setUp(self) -> None: | ||
self.client = APIClient() | ||
|
||
def test_reset_successful(self): | ||
"""Token reset is successful. 200 | ||
""" | ||
|
||
token = Token.objects.get(user=User.objects.get(username='tester')).key | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/reset_token/') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_invalid_token(self): | ||
"""Invalid token. 403 | ||
""" | ||
|
||
token = 'this-is-an-invalid-token' | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) | ||
response = self.client.post('/api/auth/reset_token/') | ||
self.assertEqual(response.status_code, 403) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
|
||
#!/usr/bin/env python3 | ||
|
||
"""Objects/Drafts_create | ||
Tests for 'Creation of BCO draft is successful.' (200), | ||
returns 207, 403 (needs to be reviewed) | ||
""" | ||
|
||
|
||
import json | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.test import APIClient | ||
|
||
class BcoDraftCreateTestCase(TestCase): | ||
fixtures = ['tests/fixtures/test_data'] | ||
def setUp(self): | ||
self.client = APIClient() | ||
|
||
self.token = Token.objects.get(user=User.objects.get(username="tester")) | ||
|
||
self.legacy_data = { | ||
"POST_api_objects_draft_create": [ | ||
{ | ||
"prefix": "BCO", | ||
"owner_group": "tester", | ||
"object_id": "http://127.0.0.1:8000/BCO_000002/DRAFT", | ||
"schema": "IEEE", | ||
"contents": { | ||
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT", | ||
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json", | ||
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea" | ||
} | ||
} | ||
] | ||
} | ||
|
||
self.data = [ | ||
{ | ||
"object_id": "http://127.0.0.1:8000/BCO_000001/DRAFT", | ||
"prefix": "BCO", | ||
"authorized_users": ["hivelab"], | ||
"contents": { | ||
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT", | ||
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json", | ||
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea" | ||
} | ||
}, | ||
{ | ||
"object_id": "http://127.0.0.1:8000/TEST_000001", | ||
"prefix": "TEST", | ||
"contents": { | ||
"object_id": "https://biocomputeobject.org/TEST_000001", | ||
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json", | ||
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea" | ||
} | ||
} | ||
] | ||
|
||
def test_legacy_successful_creation(self): | ||
"""200: Creation of BCO drafts is successful. | ||
""" | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) | ||
response = self.client.post('/api/objects/drafts/create/', self.legacy_data, format='json') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_successful_creation(self): | ||
"""200: Creation of BCO drafts is successful. | ||
""" | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) | ||
response = self.client.post('/api/objects/drafts/create/', self.data, format='json') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_partial_failure(self): | ||
# Test case for partial failure (response code 300) | ||
##Returns 207(Multi status) instead of 300(Partial faliure) | ||
data = { | ||
'POST_api_objects_draft_create': [ | ||
{ | ||
'prefix': 'BCO', | ||
'owner_group': 'bco_drafter', | ||
'schema': 'IEEE', | ||
'contents': {} | ||
}, | ||
{ | ||
'prefix': 'Reeyaa', | ||
'owner_group': 'bco_drafter', | ||
'schema': 'IEEE', | ||
'contents': {} | ||
} | ||
] | ||
} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) | ||
response = self.client.post('/api/objects/drafts/create/', data=data, format='json') | ||
self.assertEqual(response.status_code, 207) | ||
|
||
def test_bad_request(self): | ||
# Test case for bad request (response code 400) | ||
#Gives 403 forbidden request instead of 400 | ||
data = [ | ||
{ | ||
"object_id": "http://127.0.0.1:8000/TEST_000001", | ||
"prefix": "TEST", | ||
"contents": { | ||
"object_id": "https://biocomputeobject.org/TEST_000001", | ||
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json", | ||
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea" | ||
} | ||
} | ||
] | ||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) | ||
response = self.client.post('/api/objects/drafts/create/', data=data, format='json') | ||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_invalid_token(self): | ||
# Test case for invalid token (response code 403) | ||
# Setting authentication token to an invalid value | ||
|
||
data = { | ||
'POST_api_objects_draft_create': [ | ||
{ | ||
'prefix': 'BCO', | ||
'owner_group': 'bco_drafter', | ||
'schema': 'IEEE', | ||
'contents': {} | ||
}, | ||
|
||
] | ||
} | ||
self.client.credentials(HTTP_AUTHORIZATION='Token InvalidToken') | ||
response = self.client.post('/api/objects/drafts/create/', data=data, format='json') | ||
self.assertEqual(response.status_code, 403) |
Oops, something went wrong.