1+ #!/usr/bin/env python3
2+
3+ """Search the BCODB
4+ Tests for endpoint for use of query string based search.
5+ Four parameters are defined by this API:
6+ 1. contents: Search in the contents of the BCO
7+ 2. prefix: BCO Prefix to search
8+ 3. owner_user: Search by BCO owner
9+ 4. object_id: BCO object_id to search for
10+ """
11+
12+ from django .test import TestCase
13+ from rest_framework .test import APIClient
14+ from rest_framework .authtoken .models import Token
15+ from django .contrib .auth .models import User
16+ from rest_framework .test import APITestCase
17+
18+ class ObjectsTestCase (APITestCase ):
19+ fixtures = ['tests/fixtures/test_data' ]
20+
21+ def setUp (self ):
22+ self .client = APIClient ()
23+
24+ def test_search_contents (self ):
25+ """Search successfull. 200
26+ """
27+
28+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
29+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
30+ response = self .client .get ('http://localhost:8000/api/objects/?contents=review' )
31+ self .assertEqual (response .status_code , 200 )
32+ self .assertEqual (len (response .json ()[0 ]), 12 )
33+
34+ def test_search_prefix (self ):
35+ """Search successfull. 200
36+ """
37+
38+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
39+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
40+ response = self .client .get ('http://localhost:8000/api/objects/?prefix=TEST' )
41+ self .assertEqual (response .status_code , 200 )
42+ self .assertEqual (len (response .json ()[0 ]), 2 )
43+
44+ def test_search_owner_user (self ):
45+ """Search successfull. 200
46+ """
47+
48+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
49+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
50+ response = self .client .get ('http://localhost:8000/api/objects/?owner_user=test50' )
51+ self .assertEqual (response .status_code , 200 )
52+ self .assertEqual (len (response .json ()[0 ]), 4 )
53+
54+ def test_search_object_id (self ):
55+ """Search successfull. 200
56+ """
57+
58+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
59+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
60+ response = self .client .get ('http://localhost:8000/api/objects/?object_id=DRAFT' )
61+ self .assertEqual (response .status_code , 200 )
62+ self .assertEqual (len (response .json ()[0 ]), 6 )
63+
64+ def test_search_all (self ):
65+ """Search successfull. 200
66+ """
67+
68+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
69+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
70+ response = self .client .get ('http://localhost:8000/api/objects/?' )
71+ self .assertEqual (response .status_code , 200 )
72+ self .assertEqual (len (response .json ()[0 ]), 12 )
73+
74+ def test_search_multi_value (self ):
75+ """Search successfull. 200
76+ """
77+
78+ token = Token .objects .get (user = User .objects .get (username = 'bco_api_user' )).key
79+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
80+ response = self .client .get ('http://localhost:8000/api/objects/?contents=HCV&contents=DRAFT' )
81+ self .assertEqual (response .status_code , 200 )
82+ self .assertEqual (len (response .json ()[0 ]), 2 )
0 commit comments