1
|
|
|
import unittest |
2
|
|
|
import json |
3
|
|
|
import os |
4
|
|
|
import pytest |
5
|
|
|
|
6
|
|
|
from config import TestingConfig |
7
|
|
|
from run import create_app |
8
|
|
|
from resources.user import User, UserResource |
9
|
|
|
from flask import request, jsonify |
10
|
|
|
|
11
|
|
|
@pytest.mark.unittest |
12
|
|
|
class UserTest(unittest.TestCase): |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
self.app = create_app('testing') |
16
|
|
|
self.client = self.app.test_client |
17
|
|
|
self.ctx = self.app.test_request_context |
18
|
|
|
self.req = { "id": 4,"fname": "Mary", "lname": "Doe", "username": "[email protected]","password":"test" } |
19
|
|
|
self.modified = { "id": 3,"fname": "Mary", "lname": "Doe", "email": "[email protected]" } |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def test_api_can_get_all_users(self): |
24
|
|
|
"""Test api Get all the users""" |
25
|
|
|
response = self.client().get('/api/v1/user/') |
26
|
|
|
self.assertTrue(response.status_code, 200) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_api_can_get_users_by_id(self): |
30
|
|
|
"""Test api can get a users by id""" |
31
|
|
|
rv = self.client().post('/api/v1/user/', |
32
|
|
|
data = self.req) |
33
|
|
|
|
34
|
|
|
res = self.client().get('/api/v1/user/3') |
35
|
|
|
self.assertEquals(res.status_code, 200) |
36
|
|
|
|
37
|
|
|
def test_api_users_can_be_modified(self): |
38
|
|
|
#Test api can modify a users |
39
|
|
|
|
40
|
|
|
res = self.client().put('/api/v1/user/3', |
41
|
|
|
data = json.dumps(dict(self.modified))) |
42
|
|
|
self.assertEquals(res.status_code, 201) |
43
|
|
|
self.assertIn('[email protected]', str(res.data)) |
44
|
|
|
|
45
|
|
|
def test_api_can_create_users(self): |
46
|
|
|
"""Test api can create a users""" |
47
|
|
|
res = self.client().post('/api/v1/user/', data = self.req) |
48
|
|
|
self.assertEquals(res.status_code, 200) |
49
|
|
|
self.assertIn('mary', str(res.data)) |
50
|
|
|
|
51
|
|
|
def test_api_can_delete_user(self): |
52
|
|
|
|
53
|
|
|
res = self.client().delete('/api/v1/user/1') |
54
|
|
|
self.assertEquals(res.status_code, 201) |