Debian Codegraph @

examples/fsqlalchemy1/tests/test_api.py

raw
1 # Copyright 2019-2022 by J. Christopher Wagner (jwag). All rights reserved.
2 # flake8: noqa: F402
3 import pytest
4
5 pytest.importorskip("flask_sqlalchemy")
6
7 from fsqlalchemy1.app import Blog
8
9 from .test_utils import set_current_user
10
11
12 def test_monitor_404(myapp):
13 ds = myapp.security.datastore
14 with myapp.app_context():
15 ds.db.create_all()
16
17 r1 = ds.create_role(name="basic")
18 ds.create_user(email="[email protected]", password="password", roles=[r1])
19 ds.commit()
20
21 set_current_user(myapp, ds, "[email protected]")
22
23 # This requires "monitor" role
24 resp = myapp.test_client().get(
25 "/ops",
26 headers={myapp.config["SECURITY_TOKEN_AUTHENTICATION_HEADER"]: "token"},
27 )
28 assert resp.status_code == 403
29 with myapp.app_context():
30 ds.db.engine.dispose()
31
32
33 def test_blog_write(myapp):
34 ds = myapp.security.datastore
35 with myapp.app_context():
36 ds.db.create_all()
37
38 r1 = ds.create_role(name="user", permissions={"user-read", "user-write"})
39 user = ds.create_user(email="[email protected]", password="password", roles=[r1])
40
41 b1 = Blog(id=1, text="hi blog", user=user)
42 ds.put(b1)
43 ds.commit()
44
45 set_current_user(myapp, ds, "[email protected]")
46
47 # This requires "user-write" permission
48 resp = myapp.test_client().post(
49 "/blog/1",
50 headers={myapp.config["SECURITY_TOKEN_AUTHENTICATION_HEADER"]: "token"},
51 data=dict({"text": "A new blog"}),
52 )
53 assert resp.status_code == 200
54 assert b"Yes, [email protected] can update blog" == resp.data
55
56 with myapp.app_context():
57 ds.db.engine.dispose()