Skip to content

Commit

Permalink
Merge branch 'model_refactor' into bco_perms
Browse files Browse the repository at this point in the history
  • Loading branch information
HadleyKing authored Apr 3, 2024
2 parents 11827e6 + 5a4a5c9 commit 2811005
Show file tree
Hide file tree
Showing 11 changed files with 870 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tests/fixtures/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@
},
{
"model": "auth.permission",
"pk": 54,
"pk": 67,
"fields": {
"name": "Can add BCOs with prefix NOPUB",
"content_type": 13,
Expand All @@ -487,7 +487,7 @@
},
{
"model": "auth.permission",
"pk": 55,
"pk": 68,
"fields": {
"name": "Can change BCOs with prefix NOPUB",
"content_type": 13,
Expand All @@ -496,7 +496,7 @@
},
{
"model": "auth.permission",
"pk": 56,
"pk": 69,
"fields": {
"name": "Can delete BCOs with prefix NOPUB",
"content_type": 13,
Expand All @@ -505,7 +505,7 @@
},
{
"model": "auth.permission",
"pk": 57,
"pk": 70,
"fields": {
"name": "Can publish BCOs with prefix NOPUB",
"content_type": 13,
Expand All @@ -514,7 +514,7 @@
},
{
"model": "auth.permission",
"pk": 58,
"pk": 71,
"fields": {
"name": "Can add new user",
"content_type": 11,
Expand All @@ -523,7 +523,7 @@
},
{
"model": "auth.permission",
"pk": 59,
"pk": 72,
"fields": {
"name": "Can change new user",
"content_type": 11,
Expand All @@ -532,7 +532,7 @@
},
{
"model": "auth.permission",
"pk": 60,
"pk": 73,
"fields": {
"name": "Can delete new user",
"content_type": 11,
Expand Down
60 changes: 60 additions & 0 deletions tests/test_apis/test_api_authentication/test_api_auth_add.py
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 tests/test_apis/test_api_authentication/test_api_auth_remove.py
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)
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)
135 changes: 135 additions & 0 deletions tests/test_apis/test_api_objects_drafts_create.py
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)
Loading

0 comments on commit 2811005

Please sign in to comment.