|
| 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": "NOPUB", |
| 27 | + "owner_group": "tester", |
| 28 | + "object_id": "http://127.0.0.1:8000/NOPUB_000002/DRAFT", |
| 29 | + "schema": "IEEE", |
| 30 | + "contents": { |
| 31 | + "object_id": "https://test.portal.biochemistry.gwu.edu/NOPUB_000002/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