|
|
@@ -39,47 +39,55 @@ class TestJwtAuthentication():
|
|
|
self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
|
|
|
self.user = rewrite_user(User.objects.get(username="test"))
|
|
|
|
|
|
- def test_authenticate(self):
|
|
|
- with patch('desktop.auth.api_authentications.jwt.decode') as jwt_decode:
|
|
|
-
|
|
|
- HEADERS = {
|
|
|
- 'HTTP_AUTHORIZATION':
|
|
|
+ HEADERS = {
|
|
|
+ "Authorization":
|
|
|
"Bearer eyJhbGciOiJSUzI1NiJ9.eyJhdWQiOlsid29ya2xvYWQtYXBwIiwicmFuZ2VyIl0sImV4cCI6MTYyNjI1Njg5MywiaWF0IjoxNjI2MjU2NTkzLCJpc3MiOiJDbG91ZGVyYTEiLCJqdGkiOiJpZDEiLCJzdWIiOiJ0ZXN0LXN1YmplY3QiLCJ1c2VyIjoidGVzdF91c2VyIn0.jvyVDxbWTAik0jbdUcIc9ZANNrJZUCWH-Pg7FloRhg0ZYAETd_AO3p5v_ppoMmVcPD2xBSrngA5J3_A_zPBvQ_hdDlpb0_-mCCJfGhC5tju4bI9EE9Akdn2FrrsqrvQQ8cPyGsIlvoIxrK1De4f74MmUaxfN7Hrrcue1PTY4u4IB9cWQqV9vIcX99Od5PUaNekLIee-I8gweqvfGEEsW7qWUM63nh59_TOB3LLq-YcEuaX1h_oiTATeCssjk_ee9RrJGLNyKmC0WJ4UrEWn8a_T3bwCy8CMe0zV5PSuuvPHy0FvnTo2il5SDjGimxKcbpgNiJdfblslu6i35DlfiWg"
|
|
|
}
|
|
|
-
|
|
|
- request = MagicMock(HEADERS=HEADERS)
|
|
|
|
|
|
- # Existing user
|
|
|
+ self.request = MagicMock(HEADERS=HEADERS)
|
|
|
+
|
|
|
+
|
|
|
+ def test_authenticate_existing_user(self):
|
|
|
+ with patch('desktop.auth.api_authentications.jwt.decode') as jwt_decode:
|
|
|
+
|
|
|
jwt_decode.return_value = {
|
|
|
"username": "test"
|
|
|
}
|
|
|
- assert_equal(1, User.objects.count())
|
|
|
|
|
|
- user, token = JwtAuthentication().authenticate(request=request)
|
|
|
+ user, token = JwtAuthentication().authenticate(request=self.request)
|
|
|
|
|
|
assert_equal(user, self.user)
|
|
|
assert_true(user.is_authenticated)
|
|
|
- assert_equal(1, User.objects.count())
|
|
|
+ assert_false(user.is_superuser)
|
|
|
+
|
|
|
+
|
|
|
+ def test_authenticate_new_user(self):
|
|
|
+ with patch('desktop.auth.api_authentications.jwt.decode') as jwt_decode:
|
|
|
|
|
|
- # New user
|
|
|
jwt_decode.return_value = {
|
|
|
"username": "test_new_user"
|
|
|
}
|
|
|
- assert_equal(1, User.objects.count())
|
|
|
|
|
|
- user, token = JwtAuthentication().authenticate(request=request)
|
|
|
+ assert_false(User.objects.filter(username="test_new_user").exists())
|
|
|
+
|
|
|
+ user, token = JwtAuthentication().authenticate(request=self.request)
|
|
|
|
|
|
+ assert_true(User.objects.filter(username="test_new_user").exists())
|
|
|
assert_equal(User.objects.get(username="test_new_user"), user)
|
|
|
assert_true(user.is_authenticated)
|
|
|
- assert_equal(2, User.objects.count())
|
|
|
+ assert_false(user.is_superuser)
|
|
|
+
|
|
|
+
|
|
|
+ def test_failed_authentication(self):
|
|
|
+ with patch('desktop.auth.api_authentications.jwt.decode') as jwt_decode:
|
|
|
|
|
|
# Invalid token
|
|
|
jwt_decode.side_effect = exceptions.AuthenticationFailed('JwtAuthentication: Invalid token')
|
|
|
- assert_raises(exceptions.AuthenticationFailed, JwtAuthentication().authenticate, request)
|
|
|
+ assert_raises(exceptions.AuthenticationFailed, JwtAuthentication().authenticate, self.request)
|
|
|
|
|
|
# Expired token
|
|
|
jwt_decode.side_effect = exceptions.AuthenticationFailed('JwtAuthentication: Token expired')
|
|
|
- assert_raises(exceptions.AuthenticationFailed, JwtAuthentication().authenticate, request)
|
|
|
-
|
|
|
- # TODO:
|
|
|
- # check user.token
|
|
|
+ assert_raises(exceptions.AuthenticationFailed, JwtAuthentication().authenticate, self.request)
|
|
|
+
|
|
|
+ # TODO:
|
|
|
+ # check user.token
|