前后端更新
This commit is contained in:
1 parent
a0c8721fbb
commit
24da604727
33 files changed
+1951
-580
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import pytest
|
||||
import json
|
||||
from app import create_app, db
|
||||
from app.models import User
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = create_app('testing')
|
||||
app.config['TESTING'] = True
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
||||
|
||||
with app.test_client() as client:
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
# Create admin user
|
||||
admin = User(username='admin', role='admin', email='admin@example.com')
|
||||
admin.set_password('admin123')
|
||||
db.session.add(admin)
|
||||
|
||||
# Create normal user
|
||||
user = User(username='user1', role='user', email='user1@example.com')
|
||||
user.set_password('user123')
|
||||
db.session.add(user)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
yield client
|
||||
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
|
||||
def get_headers(client, username, password):
|
||||
res = client.post('/api/user/login', json={'username': username, 'password': password})
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
return {
|
||||
'X-User-Id': str(data['id']),
|
||||
'X-Username': data['username'],
|
||||
'X-Role': data['role'],
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def test_admin_access(client):
|
||||
headers = get_headers(client, 'admin', 'admin123')
|
||||
|
||||
# Test dashboard access
|
||||
res = client.get('/api/admin/dashboard', headers=headers)
|
||||
assert res.status_code == 200
|
||||
|
||||
# Test users list access
|
||||
res = client.get('/api/admin/users', headers=headers)
|
||||
assert res.status_code == 200
|
||||
users = res.get_json()
|
||||
assert len(users) >= 2 # admin + user1
|
||||
|
||||
def test_user_access_denied_to_admin(client):
|
||||
headers = get_headers(client, 'user1', 'user123')
|
||||
|
||||
# Test dashboard access (should be denied)
|
||||
res = client.get('/api/admin/dashboard', headers=headers)
|
||||
assert res.status_code == 403
|
||||
|
||||
def test_profile_data(client):
|
||||
headers = get_headers(client, 'admin', 'admin123')
|
||||
|
||||
res = client.get('/api/user/profile', headers=headers)
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
|
||||
assert data['username'] == 'admin'
|
||||
assert data['role'] == 'admin'
|
||||
assert data['email'] == 'admin@example.com'
|
||||
Reference in new issue
Block a user