An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

  1. import base64
  2. import os
  3. import random
  4. import string
  5. import tempfile
  6. from typing import Tuple, Any
  7. import pytest
  8. from werkzeug.test import Client
  9. from atheneum import create_app, init_db, register_blueprints
  10. from atheneum.model import User
  11. from atheneum.service import user_service
  12. def add_test_user() -> Tuple[str, str]:
  13. test_username = 'test_' + ''.join(
  14. random.choices(string.ascii_letters + string.digits, k=17)).strip()
  15. test_password = ''.join(
  16. random.choices(string.ascii_letters + string.digits, k=32)).strip()
  17. user_service.register(test_username, test_password, User.ROLE_ADMIN)
  18. return test_username, test_password
  19. @pytest.fixture
  20. def app():
  21. """Create and configure a new app instance for each test."""
  22. # create a temporary file to isolate the database for each test
  23. db_fd, db_path = tempfile.mkstemp()
  24. # create the app with common test config
  25. app = create_app({
  26. 'TESTING': True,
  27. 'DATABASE': db_path,
  28. })
  29. register_blueprints(app)
  30. # create the database and load test data
  31. with app.app_context():
  32. init_db()
  33. test_username, test_password = add_test_user()
  34. app.config['test_username'] = test_username
  35. app.config['test_password'] = test_password
  36. # get_db().executescript(_data_sql)
  37. yield app
  38. # close and remove the temporary database
  39. os.close(db_fd)
  40. os.unlink(db_path)
  41. @pytest.fixture
  42. def client(app):
  43. """A test client for the app."""
  44. return app.test_client()
  45. @pytest.fixture
  46. def runner(app):
  47. """A test runner for the app's Click commands."""
  48. return app.test_cli_runner()
  49. class AuthActions(object):
  50. def __init__(self, client: Client, username: str = "", password: str = ""):
  51. self._client = client
  52. self.username: str = username
  53. self.password: str = password
  54. self.token: str = ""
  55. def configure(self, username, password) -> Any:
  56. self.username = username
  57. self.password = password
  58. return self
  59. def login(self):
  60. auth_header = self.get_authorization_header_basic()
  61. result = self._client.post(
  62. '/auth/login',
  63. headers={
  64. auth_header[0]: auth_header[1]
  65. }
  66. )
  67. self.token = result.json['token']
  68. return result
  69. def bump(self):
  70. auth_header = self.get_authorization_header_token()
  71. return self._client.post(
  72. '/auth/bump',
  73. headers={
  74. auth_header[0]: auth_header[1]
  75. }
  76. )
  77. def logout(self):
  78. auth_header = self.get_authorization_header_token()
  79. return self._client.post(
  80. '/auth/logout',
  81. headers={
  82. auth_header[0]: auth_header[1]
  83. }
  84. )
  85. def get_authorization_header_basic(self) -> Tuple[str, str]:
  86. credentials = base64.b64encode(
  87. '{}:{}'.format(self.username, self.password).encode('utf8')) \
  88. .decode('utf8').strip()
  89. return 'Authorization', 'Basic {}'.format(credentials)
  90. def get_authorization_header_token(self) -> Tuple[str, str]:
  91. credentials = base64.b64encode(
  92. '{}:{}'.format(self.username, self.token).encode('utf8')) \
  93. .decode('utf8').strip()
  94. return 'Authorization', 'Token {}'.format(credentials)
  95. @pytest.fixture
  96. def auth(client: Client):
  97. return AuthActions(client,
  98. client.application.config.get('test_username'),
  99. client.application.config.get('test_password'))