ApiTest.test_api_can_get_all_requests()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""
2
APIs endpoint test
3
test:
4
    -> gets all the requests for logged in user
5
    -> gets a request for a logged in user
6
    -> creates a request
7
    -> Modify a request
8
9
"""
10
11
import unittest
12
import json
13
import os
14
import pytest
15
16
from flask_restful import Api
17
from resources.requests import dtrequest, RequestResource, Request
18
from run import create_app
19
from app import api_bp
20
21
22
@pytest.mark.unittest
23
class ApiTest(unittest.TestCase):
24
    """ API endpoints test case"""
25
26
    def setUp(self):
27
        #Declare test variables and initialize app
28
        self.app = create_app('testing')
29
        self.client = self.app.test_client
30
        self.req = { "id": 5, "requestor":"Test Doe", "email": "[email protected]",
31
                "type": "maintenance", "status":"Approved", "desc": "Description goes here"}
32
       
33
        
34
           
35
    def tearDown(self):
36
        pass
37
        
38
39
        
40
41
    def test_api_can_get_all_requests(self):
42
        """Test api Get all the requests for a logged in user"""
43
        response = self.client().get('/api/v1/user/request')
44
        self.assertTrue(response.status_code, 200)
45
        
46
47
    def test_api_can_get_request_by_id(self):
48
        """Test api can get a request for a logged in user"""
49
        res = self.client().get('/api/v1/user/request/1')
50
        self.assertEquals(res.status_code, 200)
51
52
    def test_api_request_can_be_modified(self):
53
        #Test api can modify a request
54
        rv = self.client().post('/api/v1/user/request/', 
55
                data = json.dumps(dict({"requestor":"sue doe"})))
56
        self.assertEquals(rv.status_code, 200)
57
58
        res = self.client().put('/api/v1/user/request/1',
59
                data = json.dumps(dict({"requestor":"Susan Sue"})))
60
        self.assertEquals(rv.status_code, 200)
61
        self.assertIn('Susan Sue', str(res.data))
62
63
64
    def test_api_can_create_request(self):
65
        """Test api can create a request"""
66
        res = self.client().post('/api/v1/user/request/', data = json.dumps(dict(self.req)))
67
        self.assertEquals(res.status_code, 201)
68
        self.assertIn('Test Doe', str(res.data))
69
        
70
71
#Make tests executable
72
if __name__ == "__main__":
73
    unittest.main()