Skip to content

Commit c7724db

Browse files
Reeya123HadleyKing
authored andcommitted
Objects_drafts_publish
1 parent 8857bbd commit c7724db

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

tests/test_views/test_api_objects_drafts_publish.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#!/usr/bin/env python3
2+
3+
'''Publish BCO Draft
4+
expecting a response status code of 200,300, 400, but receiving a 207 status code (Multi-status rsponse), which might indicate that
5+
suggesting that there are multiple operations being performed in the background as a result of the request.
6+
7+
Tests for 403(Invalid token)
8+
'''
9+
110
from django.test import TestCase
211
from rest_framework.test import APIClient
312
from rest_framework.authtoken.models import Token
@@ -8,47 +17,79 @@
817
class PublishDraftBCOTestCase(TestCase):
918
fixtures = ['tests/fixtures/test_data']
1019
def setUp(self):
20+
fixtures = ['tests/fixtures/test_data']
1121
self.client = APIClient()
22+
# Checking if the user 'bco_api_user' already exists
23+
try:
24+
self.user = User.objects.get(username='bco_api_user')
25+
except User.DoesNotExist:
26+
self.user = User.objects.create_user(username='bco_api_user')
27+
28+
# Checking if user already has token, if not then creating one
29+
if not Token.objects.filter(user=self.user).exists():
30+
self.token = Token.objects.create(user=self.user)
31+
else:
32+
self.token = Token.objects.get(user=self.user)
1233

1334
def test_publish_bco_success(self):
14-
# Successful request to publish a draft BCO
35+
# Test for Successful request to publish a draft BCO
36+
#Returns 207 instead of 200
37+
1538
data = {
1639
"POST_api_objects_drafts_publish": [
1740
{
18-
"prefix": "string",
19-
"draft_id": "string",
20-
"object_id": "string",
21-
"delete_draft": True
41+
"prefix": "BCO",
42+
"draft_id": "http://127.0.0.1:8000/BCO_000000/DRAFT",
43+
44+
"delete_draft": False
45+
2246
}
2347
]
2448
}
25-
#self.client.force_authenticate(user=self.user)
49+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
2650
response = self.client.post('/api/objects/drafts/publish/', data=data, format='json')
2751
self.assertEqual(response.status_code, 200)
2852

2953
def test_publish_bco_partial_failure(self):
3054
# Some requests failed while publishing the draft BCO
55+
#Returns 207 instead of 300
56+
3157
data = {
3258
"POST_api_objects_drafts_publish": [
3359
{
34-
"prefix": "string",
35-
"draft_id": "string",
36-
"object_id": "strin",
37-
"delete_draft": True
60+
"prefix": "BCO",
61+
"draft_id": "http://127.0.0.1:8000/BCO_000001/DRAFT",
62+
63+
"delete_draft": False
3864
},
39-
# Add more objects if needed to simulate partial failures
65+
{
66+
"prefix": "InvalidPrefix",
67+
"draft_id": "InvalidDraftId",
68+
69+
"delete_draft": False
70+
}
71+
4072
]
4173
}
42-
74+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
4375
response = self.client.post('/api/objects/drafts/publish/', data=data, format='json')
4476
self.assertEqual(response.status_code, 300)
4577

4678
def test_publish_bco_bad_request(self):
4779
# Bad request: Invalid or missing data
80+
#Returns 207 instead of 400
81+
4882
data = {
49-
# Missing required fields or invalid data
83+
"POST_api_objects_drafts_publish": [
84+
{
85+
"prefix": "BCO",
86+
"draft_id": "InvalidID",
87+
"delete_draft": False
88+
},
89+
90+
]
5091
}
51-
#self.client.force_authenticate(user=self.user)
92+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
5293
response = self.client.post('/api/objects/drafts/publish/', data=data, format='json')
5394
self.assertEqual(response.status_code, 400)
5495

@@ -57,10 +98,11 @@ def test_publish_bco_invalid_token(self):
5798
data = {
5899
"POST_api_objects_drafts_publish": [
59100
{
60-
"prefix": "string",
61-
"draft_id": "string",
62-
"object_id": "string",
63-
"delete_draft": True
101+
"prefix": "BCO",
102+
"draft_id": "http://127.0.0.1:8000/BCO_000000/DRAFT",
103+
104+
"delete_draft": False
105+
64106
}
65107
]
66108
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from django.test import TestCase
2+
from rest_framework.test import APIClient
3+
from rest_framework.authtoken.models import Token
4+
from django.contrib.auth.models import User
5+
from rest_framework.test import APITestCase
6+
7+
class ObjectsSearchTestCase(APITestCase):
8+
def setUp(self):
9+
fixtures = ['tests/fixtures/test_data']
10+
self.client = APIClient()
11+
# Checking if the user 'bco_api_user' already exists
12+
try:
13+
self.user = User.objects.get(username='bco_api_user')
14+
except User.DoesNotExist:
15+
self.user = User.objects.create_user(username='bco_api_user')
16+
17+
# Checking if user already has token, if not then creating one
18+
if not Token.objects.filter(user=self.user).exists():
19+
self.token = Token.objects.create(user=self.user)
20+
else:
21+
self.token = Token.objects.get(user=self.user)
22+
23+
def test_search_successful(self):
24+
# Test case for a successful search (status code: 200)
25+
###Gives 404 instead of 200.
26+
data = {
27+
"POST_api_objects_search": [
28+
{
29+
"type": "prefix",
30+
"search": "TEST"
31+
}
32+
]
33+
}
34+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
35+
response = self.client.post("/api/objects/search/", data=data, format="json")
36+
37+
self.assertEqual(response.status_code, 200)
38+
39+
40+
41+
def test_prefix_not_found(self):
42+
# Test case for prefix not found (status code: 404)
43+
data = {
44+
"POST_api_objects_search": [
45+
{
46+
"type": "prefix",
47+
"search": "invalid prefix"
48+
}
49+
]
50+
}
51+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
52+
53+
response = self.client.post("/api/objects/search/", data=data, format="json")
54+
55+
self.assertEqual(response.status_code, 404)

0 commit comments

Comments
 (0)