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.

132 lines
5.3 KiB

  1. from mock import patch, MagicMock, Mock
  2. from atheneum.middleware.authentication_middleware import \
  3. authenticate_with_password, authenticate_with_token
  4. middleware_module = 'atheneum.middleware.authentication_middleware'
  5. @patch(middleware_module + '.g')
  6. @patch(middleware_module + '.authentication_service.is_valid_password')
  7. @patch(middleware_module + '.user_service.find_by_name')
  8. def test_authenticate_with_password_happy_path(
  9. mock_user_service: MagicMock,
  10. mock_authentication_service: MagicMock,
  11. mock_g: MagicMock):
  12. mock_g.user = Mock()
  13. mock_user_service.return_value = Mock()
  14. mock_authentication_service.return_value = True
  15. assert authenticate_with_password('test', 'test')
  16. mock_user_service.assert_called_once()
  17. mock_authentication_service.assert_called_once()
  18. mock_g.user.assert_not_called()
  19. @patch(middleware_module + '.g')
  20. @patch(middleware_module + '.authentication_service.is_valid_password')
  21. @patch(middleware_module + '.user_service.find_by_name')
  22. def test_authenticate_with_password_no_user(
  23. mock_user_service: MagicMock,
  24. mock_authentication_service: MagicMock,
  25. mock_g: MagicMock):
  26. mock_g.user = Mock()
  27. mock_user_service.return_value = None
  28. mock_authentication_service.return_value = True
  29. assert not authenticate_with_password('test', 'test')
  30. mock_user_service.assert_called_once()
  31. mock_authentication_service.assert_not_called()
  32. mock_g.user.assert_not_called()
  33. @patch(middleware_module + '.g')
  34. @patch(middleware_module + '.authentication_service.is_valid_password')
  35. @patch(middleware_module + '.user_service.find_by_name')
  36. def test_authenticate_with_password_invalid_password(
  37. mock_user_service: MagicMock,
  38. mock_authentication_service: MagicMock,
  39. mock_g: MagicMock):
  40. mock_g.user = Mock()
  41. mock_user_service.return_value = Mock()
  42. mock_authentication_service.return_value = False
  43. assert not authenticate_with_password('test', 'test')
  44. mock_user_service.assert_called_once()
  45. mock_authentication_service.assert_called_once()
  46. mock_g.user.assert_not_called()
  47. @patch(middleware_module + '.g')
  48. @patch(middleware_module + '.authentication_service.is_valid_token')
  49. @patch(middleware_module + '.user_token_service.find_by_user_and_token')
  50. @patch(middleware_module + '.user_service.find_by_name')
  51. def test_authenticate_with_token_happy_path(
  52. mock_user_service: MagicMock,
  53. mock_user_token_service: MagicMock,
  54. mock_authentication_service: MagicMock,
  55. mock_g: MagicMock):
  56. mock_g.user = Mock()
  57. mock_user_service.return_value = Mock()
  58. mock_user_token_service.return_value = Mock()
  59. mock_authentication_service.return_value = True
  60. assert authenticate_with_token('test', 'test')
  61. mock_user_service.assert_called_once()
  62. mock_user_token_service.assert_called_once()
  63. mock_authentication_service.assert_called_once()
  64. mock_g.user.assert_not_called()
  65. @patch(middleware_module + '.g')
  66. @patch(middleware_module + '.authentication_service.is_valid_token')
  67. @patch(middleware_module + '.user_token_service.find_by_user_and_token')
  68. @patch(middleware_module + '.user_service.find_by_name')
  69. def test_authenticate_with_token_no_user(
  70. mock_user_service: MagicMock,
  71. mock_user_token_service: MagicMock,
  72. mock_authentication_service: MagicMock,
  73. mock_g: MagicMock):
  74. mock_g.user = Mock()
  75. mock_user_service.return_value = None
  76. assert not authenticate_with_token('test', 'test')
  77. mock_user_service.assert_called_once()
  78. mock_user_token_service.assert_not_called()
  79. mock_authentication_service.assert_not_called()
  80. mock_g.user.assert_not_called()
  81. @patch(middleware_module + '.g')
  82. @patch(middleware_module + '.authentication_service.is_valid_token')
  83. @patch(middleware_module + '.user_token_service.find_by_user_and_token')
  84. @patch(middleware_module + '.user_service.find_by_name')
  85. def test_authenticate_with_token_no_user_token(
  86. mock_user_service: MagicMock,
  87. mock_user_token_service: MagicMock,
  88. mock_authentication_service: MagicMock,
  89. mock_g: MagicMock):
  90. mock_g.user = Mock()
  91. mock_user_service.return_value = Mock()
  92. mock_user_token_service.return_value = None
  93. mock_authentication_service.return_value = False
  94. assert not authenticate_with_token('test', 'test')
  95. mock_user_service.assert_called_once()
  96. mock_user_token_service.assert_called_once()
  97. mock_authentication_service.assert_called_once()
  98. mock_g.user.assert_not_called()
  99. @patch(middleware_module + '.g')
  100. @patch(middleware_module + '.authentication_service.is_valid_token')
  101. @patch(middleware_module + '.user_token_service.find_by_user_and_token')
  102. @patch(middleware_module + '.user_service.find_by_name')
  103. def test_authenticate_with_token_invalid_token(
  104. mock_user_service: MagicMock,
  105. mock_user_token_service: MagicMock,
  106. mock_authentication_service: MagicMock,
  107. mock_g: MagicMock):
  108. mock_g.user = Mock()
  109. mock_user_service.return_value = Mock()
  110. mock_user_token_service.return_value = Mock()
  111. mock_authentication_service.return_value = False
  112. assert not authenticate_with_token('test', 'test')
  113. mock_user_service.assert_called_once()
  114. mock_user_token_service.assert_called_once()
  115. mock_authentication_service.assert_called_once()
  116. mock_g.user.assert_not_called()