-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
103 lines (89 loc) · 3.36 KB
/
test_app.py
File metadata and controls
103 lines (89 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pytest
from app import app, db
from models import User, Account
from flask_jwt_extended import create_access_token
from config import Config
@pytest.fixture(scope='module')
def test_client():
with app.test_client() as client:
with app.app_context():
db.create_all()
yield client
db.session.remove()
db.drop_all()
@pytest.fixture(scope='module')
def admin_user():
admin = User(username="admin_user")
admin.set_password("adminpass")
admin.role = "admin"
db.session.add(admin)
db.session.commit()
return admin
@pytest.fixture(scope='module')
def normal_user():
user = User(username="test_user")
user.set_password("testpass")
db.session.add(user)
db.session.commit()
return user
def get_headers(user):
token = create_access_token(identity=user.id)
return {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
def test_signup(test_client):
response = test_client.post('/signup', json={
"username": "new_user",
"password": "newpass"
})
assert response.status_code == 201
assert response.json["message"] == "User created successfully"
def test_login(test_client, normal_user):
response = test_client.post('/login', json={
"username": "test_user",
"password": "testpass"
})
assert response.status_code == 200
assert "access_token" in response.json
def test_profile(test_client, normal_user):
headers = get_headers(normal_user)
response = test_client.get('/profile', headers=headers)
assert response.status_code == 200
assert response.json["username"] == "test_user"
def test_add_account_as_admin(test_client, admin_user):
headers = get_headers(admin_user)
response = test_client.post('/accounts', headers=headers, json={
"name": "Test Account",
"email": "testaccount@example.com",
"contact_number": "1234567890"
})
assert response.status_code == 201
assert response.json["message"] == "Account created successfully"
def test_update_account_as_admin(test_client, admin_user):
headers = get_headers(admin_user)
account = Account.query.first()
response = test_client.put(f'/accounts/{account.id}', headers=headers, json={
"name": "Updated Account",
"email": "updated@example.com"
})
assert response.status_code == 200
assert response.json["message"] == "Account updated successfully"
def test_get_account(test_client, admin_user):
headers = get_headers(admin_user)
response = test_client.get('/accounts', headers=headers)
assert response.status_code == 200
assert "accounts" in response.json
def test_delete_account(test_client, admin_user):
headers = get_headers(admin_user)
account = Account.query.first()
response = test_client.delete(f'/accounts/{account.id}', headers=headers)
assert response.status_code == 200
assert response.json["message"] == "Account deleted successfully"
def test_update_user_role_as_super_admin(test_client, admin_user):
headers = {'Authorization': Config.SU_ADMIN_ID, 'Content-Type': 'application/json'}
response = test_client.patch(f'/user/{admin_user.id}', headers=headers, json={
"role": "client"
})
assert response.status_code == 200
assert response.json["message"] == "User role updated to client successfully"