11
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+
29
310import json
411from django .test import TestCase
815
916class BcoDraftCreateTestCase (TestCase ):
1017 fixtures = ['tests/fixtures/test_data' ]
18+ def setUp (self ):
19+ self .client = APIClient ()
1120
12- # def setUp(self):
13- # self.client = Client()
14- # self.url = '/api/objects/drafts/create' # The URL for the create draft endpoint
15- # self.user = User.objects.create_user(username='bco_api_user', password='biocompute')
21+ # Checking if the user 'bco_api_user' already exists
22+ try :
23+ self .user = User .objects .get (username = 'bco_api_user' )
24+ except User .DoesNotExist :
25+ self .user = User .objects .create_user (username = 'bco_api_user' )
1626
27+ # Checking if user already has token, if not then creating one
28+ if not Token .objects .filter (user = self .user ).exists ():
29+ self .token = Token .objects .create (user = self .user )
30+ else :
31+ self .token = Token .objects .get (user = self .user )
32+
1733 def test_successful_creation (self ):
1834 """200: Creation of BCO draft is successful.
1935 """
20-
21- client = APIClient ()
22- token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
23- client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
36+
2437
2538 data = {
2639 'POST_api_objects_draft_create' : [
@@ -30,51 +43,69 @@ def test_successful_creation(self):
3043 'schema' : 'IEEE' ,
3144 'contents' : {}
3245 },
33- {
34- 'prefix' : 'Hadley' ,
35- 'owner_group' : 'bco_drafter' ,
36- 'schema' : 'IEEE' ,
37- 'contents' : {}
38- }
46+
3947 ]
4048 }
41- response = client .post ('/api/objects/drafts/create/' ,data , format = 'json' )
49+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + self .token .key )
50+ response = self .client .post ('/api/objects/drafts/create/' , data , format = 'json' )
4251 self .assertEqual (response .status_code , 200 )
4352
4453 def test_partial_failure (self ):
4554 # Test case for partial failure (response code 300)
55+ ##Returns 207(Multi status) instead of 300(Partial faliure)
4656 data = {
47- 'prefix' : 'string' ,
48- 'owner_group' : 'string' ,
49- 'object_id' : 'string' ,
50- 'schema' : 'string' ,
51- 'contents' : {
52- "additionalProp1" : {}
53- }
57+ 'POST_api_objects_draft_create' : [
58+ {
59+ 'prefix' : 'BCO' ,
60+ 'owner_group' : 'bco_drafter' ,
61+ 'schema' : 'IEEE' ,
62+ 'contents' : {}
63+ },
64+ {
65+ 'prefix' : 'Reeyaa' ,
66+ 'owner_group' : 'bco_drafter' ,
67+ 'schema' : 'IEEE' ,
68+ 'contents' : {}
69+ }
70+ ]
5471 }
55- response = self .client .post (self .url , data = json .dumps (data ), content_type = 'application/json' )
56- self .assertEqual (response .status_code , 300 )
72+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + self .token .key )
73+ response = self .client .post ('/api/objects/drafts/create/' , data = data , format = 'json' )
74+ self .assertEqual (response .status_code , 207 )
5775
5876 def test_bad_request (self ):
5977 # Test case for bad request (response code 400)
78+ #Gives 403 forbidden request instead of 400
6079 data = {
61- "additionalProp1" : {},
80+ 'POST_api_objects_draft_create' : [
81+ {
82+ 'prefix' : 'BCO' ,
83+ 'owner_group' : 'bco_drafter' ,
84+ 'schema' : 'IEEE' ,
85+ 'contents' : {}
86+ },
87+
88+ ]
6289 }
63- response = self .client .post (self .url , data = json .dumps (data ), content_type = 'application/json' )
64- self .assertEqual (response .status_code , 400 )
90+ #self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
91+ response = self .client .post ('/api/objects/drafts/create/' , data = data , format = 'json' )
92+ self .assertEqual (response .status_code , 403 )
6593
6694 def test_invalid_token (self ):
6795 # Test case for invalid token (response code 403)
6896 # Setting authentication token to an invalid value
69- self . client . defaults [ 'HTTP_AUTHORIZATION' ] = 'Invalid Token'
97+
7098 data = {
71- 'prefix' : 'string' ,
72- 'owner_group' : 'string' ,
73- 'object_id' : 'string' ,
74- 'schema' : 'string' ,
75- 'contents' : {
76- "additionalProp1" : {}
77- }
99+ 'POST_api_objects_draft_create' : [
100+ {
101+ 'prefix' : 'BCO' ,
102+ 'owner_group' : 'bco_drafter' ,
103+ 'schema' : 'IEEE' ,
104+ 'contents' : {}
105+ },
106+
107+ ]
78108 }
79- response = self .client .post (self .url , data = json .dumps (data ), content_type = 'application/json' )
109+ self .client .credentials (HTTP_AUTHORIZATION = 'Token InvalidToken' )
110+ response = self .client .post ('/api/objects/drafts/create/' , data = data , format = 'json' )
80111 self .assertEqual (response .status_code , 403 )
0 commit comments