1
|
|
|
import os |
2
|
|
|
import unittest |
3
|
|
|
import pytest |
4
|
|
|
import psycopg2 |
5
|
|
|
|
6
|
|
|
from flask import json, request, jsonify |
7
|
|
|
from run import create_app |
8
|
|
|
from tests.models import create_tables |
9
|
|
|
|
10
|
|
|
@pytest.mark.unittest |
11
|
|
|
|
12
|
|
|
class AuthTest(unittest.TestCase): |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
self.app = create_app('testing') |
16
|
|
|
self.client = self.app.test_client |
17
|
|
|
self.reg = { "username": "[email protected]", "firstname": "Mary", |
18
|
|
|
"lastname": "Doe","password": "Doe","created_on": "Doe" } |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
with self.app.app_context(): |
22
|
|
|
create_tables() |
23
|
|
|
|
24
|
|
|
def tearDown(self): |
25
|
|
|
pass |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def test_encode_auth_token(self): |
29
|
|
|
""" |
30
|
|
|
user = UserAuth( |
31
|
|
|
email = '[email protected]', |
32
|
|
|
password = 'test' |
33
|
|
|
) |
34
|
|
|
auth_token = user.encode_auth_token(user.id) |
35
|
|
|
""" |
36
|
|
|
|
37
|
|
|
@pytest.mark.skip("to run after implementation") |
38
|
|
|
def test_registration_endpoint(self): |
39
|
|
|
"""Test signup/register users endpoint""" |
40
|
|
|
res = self.client().post('/api/v1/auth/signup/', data = json.dumps(dict(self.reg))) |
41
|
|
|
self.assertEquals(res.status_code, 201) |
42
|
|
|
self.assertIn('mary', str(res.data)) |
43
|
|
|
|
44
|
|
|
@pytest.mark.skip("to run after implementation") |
45
|
|
|
def test_login_endpoint(self): |
46
|
|
|
"""Test login endpoint""" |
47
|
|
|
res = self.client().post('/api/v1/auth/login/', data = json.dumps(dict(self.reg))) |
48
|
|
|
self.assertEquals(res.status_code,201) |
49
|
|
|
|
50
|
|
|
@pytest.mark.skip("to run after implementation") |
51
|
|
|
def test_user_authentication(self): |
52
|
|
|
"""Tests login and logout""" |
53
|
|
|
json_data = request.get_json() |
54
|
|
|
email = json_data['email'] |
55
|
|
|
password = json_data['password'] |
56
|
|
|
return jsonify(token={email, password}) |
57
|
|
|
|
58
|
|
|
with self.client() as c: |
59
|
|
|
rv = c.post('/api/auth', json={ |
60
|
|
|
'username': 'flask', 'password': 'secret' |
61
|
|
|
}) |
62
|
|
|
json_data = rv.get_json() |
63
|
|
|
assert(json_data['email'], json_data['token']) |