Skip to content

Commit

Permalink
Cleaning up some things
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HadleyKing committed Mar 20, 2024
1 parent 1b20196 commit 2530f7d
Show file tree
Hide file tree
Showing 11 changed files with 449 additions and 17 deletions.
22 changes: 12 additions & 10 deletions biocompute/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
from config.services import legacy_api_converter, response_constructor
from biocompute.services import BcoDraftSerializer

class DraftsCreateApi(APIView):
"""
Create BCO Draft [Bulk Enabled]
--------------------
Creates a new BCO draft object.
"""

request_body = openapi.Schema(
BCO_DRAFT_SCHEMA = openapi.Schema(
type=openapi.TYPE_ARRAY,
title="Create BCO Draft Schema",
items=openapi.Schema(
Expand Down Expand Up @@ -61,6 +52,17 @@ class DraftsCreateApi(APIView):
description="BCO Drafts to create.",
)

class DraftsCreateApi(APIView):
"""
Create BCO Draft [Bulk Enabled]
--------------------
Creates a new BCO draft object.
"""

request_body = BCO_DRAFT_SCHEMA

@swagger_auto_schema(
request_body=request_body,
responses={
Expand Down
4 changes: 2 additions & 2 deletions biocompute/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.13 on 2024-03-14 13:52
# Generated by Django 3.2.13 on 2024-03-20 18:48

from django.conf import settings
from django.db import migrations, models
Expand All @@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('prefix', '0001_initial'),
('auth', '0012_alter_user_first_name_max_length'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

Expand Down
6 changes: 3 additions & 3 deletions prefix/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class PrefixesCreateApi(APIView):
"""
Create a Prefix
Create a Prefix [Bulk Enabled]
--------------------
Create a prefix to be used to classify BCOs and to determine permissions
Expand Down Expand Up @@ -119,7 +119,7 @@ def post(self, request) -> Response:

class PrefixesDeleteApi(APIView):
"""
Delete a Prefix
Delete a Prefix [Bulk Enabled]
# Deletes a prefix for BCOs.
--------------------
Expand Down Expand Up @@ -209,7 +209,7 @@ def post(self, request) -> Response:

class PrefixesModifyApi(APIView):
"""
Modify a Prefix
Modify a Prefix [Bulk Enabled]
--------------------
Expand Down
5 changes: 3 additions & 2 deletions prefix/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.13 on 2024-03-14 13:14
# Generated by Django 3.2.13 on 2024-03-20 18:48

from django.conf import settings
from django.db import migrations, models
Expand All @@ -11,8 +11,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
Expand All @@ -23,6 +23,7 @@ class Migration(migrations.Migration):
('certifying_key', models.TextField(blank=True, null=True)),
('created', models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True)),
('description', models.TextField(blank=True, null=True)),
('counter', models.IntegerField(default=0, help_text='Counter for object_id asignment')),
('authorized_groups', models.ManyToManyField(blank=True, related_name='authorized_prefix', to='auth.Group')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username')),
],
Expand Down
50 changes: 50 additions & 0 deletions tests/test_views/test_account_activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3

"""Test Account Activation
Test for '200: Account has been authorized.', '404: Credentials not found.',
and '403: Requestor's credentials were rejected.'
"""

import time
from django.test import TestCase, Client

class ApiAccountsActivateTestCase(TestCase):
fixtures = ['tests/fixtures/test_data']

def setUp(self):
self.client = Client()

def test_account_activated_success(self):
"""Test for '201: Account creation request is successful.'
"""

response = self.client.get(
'/api/accounts/activate/'\
+'test_new_user%40testing.com/sample_temp_identifier'
)
self.assertEqual(response.status_code, 200)

def test_account_activated_forbidden(self):
"""Test for '403: Requestor's credentials were rejected.'
"""

bad_link = "test_new_user%40testing.com/bad_temp_identifier"
response = self.client.get(f'/api/accounts/activate/{bad_link}')
self.assertEqual(response.status_code, 403)

def test_account_activated_not_found(self):
"""Test for '404: That account, {email}, was not found'
"""

bad_link = "test22%40testing.com/sample_temp_identifier"
response = self.client.get(f'/api/accounts/activate/{bad_link}')
self.assertEqual(response.status_code, 404)

def test_account_activated_conflict(self):
"""Test for '409: CONFLICT: That account, {email},
has already been activated.'
"""

bad_link = "tester%40testing.com/sample_temp_identifier"
response = self.client.get(f'/api/accounts/activate/{bad_link}')
self.assertEqual(response.status_code, 409)
40 changes: 40 additions & 0 deletions tests/test_views/test_account_describe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

"""API- Accounts describe
Tests for 'Authorization is successfull' (200),
'Forbidden. Authentication credentials were not provided' (403),
'Invalid Token' (403)
"""


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 AccountDescribeTestCase(TestCase):
fixtures = ['tests/fixtures/test_data']

def test_success_response(self):
"""200: Authorization is successful.
"""
client = APIClient()
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
client.credentials(HTTP_AUTHORIZATION='Token ' + token)
response = client.post('/api/accounts/describe/', format='json')
self.assertEqual(response.status_code, 200)

def test_forbidden_response(self):
"""403: Forbidden. Authentication credentials were not provided.
"""
client = APIClient()
response = client.post('/api/accounts/describe/')
self.assertEqual(response.status_code, 403)

def test_unauthorized_response(self):
"""403: Invalid token
"""
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token This-token-is-bad')
response = client.post('/api/accounts/describe/')
self.assertEqual(response.status_code, 403)
57 changes: 57 additions & 0 deletions tests/test_views/test_account_new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

"""New Account
Test for '201: Account creation request is successful.', '400: Bad
request format.', and '409: Account has already been authenticated or
requested.'
"""

from django.test import TestCase, Client

class ApiAccountsNewTestCase(TestCase):
fixtures = ['tests/fixtures/test_data']

def setUp(self):
self.client = Client()

def test_creation_request_success(self):
""" Test for '201: Account creation request is successful.'
"""

data = {
'hostname': 'http://localhost:8000',
'email': '[email protected]',
'token': 'SampleToken'
}


response = self.client.post('/api/accounts/new/', data=data)
self.assertEqual(response.status_code, 201)

def test_creation_request_success_bad_request(self):
"""Test for '400: Bad request format.'
"""
data = {
'hostname': 'UserDB',
'email': '[email protected]'
}

response = self.client.post('/api/accounts/new/', data=data)
self.assertEqual(response.status_code, 400)

def test_creation_request_conflict(self):
""" Test for '409: Account has already been authenticated or
requested.'
"""

data = {
'hostname': 'http://localhost:8000',
'email': '[email protected]',
'token': 'SampleToken'
}


response = self.client.post('/api/accounts/new/', data=data)
response2 = self.client.post('/api/accounts/new/', data=data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response2.status_code, 409)
60 changes: 60 additions & 0 deletions tests/test_views/test_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_views/test_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)
Loading

0 comments on commit 2530f7d

Please sign in to comment.