def test_is_authenticated_external_auth(self): Config.objects.set_config('external_auth_url', 'https://example.com') user = factory.make_User() request = factory.make_fake_request('/') request.user = user auth = MAASAPIAuthentication() self.assertTrue(auth.is_authenticated(request))
def test_is_authenticated_false_external_user_no_external_auth(self): Config.objects.set_config('external_auth_url', '') user = factory.make_User() user.userprofile.is_local = False user.userprofile.save() request = self.make_request(user=user) auth = MAASAPIAuthentication() self.assertFalse(auth.is_authenticated(request))
def test_is_authenticated_external_auth(self): user = factory.make_User() request = factory.make_fake_request('/') request.external_auth_info = ExternalAuthInfo('macaroon', 'https://example.com') request.user = user auth = MAASAPIAuthentication() self.assertTrue(auth.is_authenticated(request))
def test_is_authenticated_external_auth_user_local(self, mock_validate): mock_validate.return_value = True auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.is_local = True user.userprofile.save() request = self.make_request() self.assertFalse(auth.is_authenticated(request)) mock_validate.assert_not_called()
def test_is_authenticated_false_external_user_no_external_auth(self): Config.objects.set_config("external_auth_url", "") user = factory.make_User() user.userprofile.is_local = False user.userprofile.save() mock_token = mock.Mock(user=user) request = self.make_request() auth = MAASAPIAuthentication() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertFalse(auth.is_authenticated(request))
def test_is_authenticated_external_auth_system_user(self): mock_validate = self.patch(api_auth, "validate_user_external_auth") mock_validate.return_value = True auth = MAASAPIAuthentication() user = get_node_init_user() request = self.make_request() mock_token = mock.Mock(user=user) auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertTrue(auth.is_authenticated(request)) mock_validate.assert_not_called()
def test_is_authenticated_external_auth_validate_fail(self, mock_validate): mock_validate.return_value = False auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.auth_last_check -= timedelta(days=1) mock_token = mock.Mock(user=user) request = self.make_request() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertFalse(auth.is_authenticated(request)) # check interval not expired, the user isn't checked mock_validate.assert_called()
def test_is_authenticated_external_auth_validate_user(self, mock_validate): mock_validate.return_value = True auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.auth_last_check = ( datetime.utcnow() - timedelta(days=1)) mock_token = mock.Mock(user=user) request = self.make_request() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertTrue(auth.is_authenticated(request)) mock_validate.assert_called_with(user, admin_group='admins')
def test_is_authenticated_external_auth_validate_user(self, mock_validate): mock_validate.return_value = True auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.auth_last_check -= timedelta(days=1) mock_token = mock.Mock(user=user) request = factory.make_fake_request('/') request.external_auth_info = ExternalAuthInfo('macaroon', 'https://example.com') request.user = AnonymousUser() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertTrue(auth.is_authenticated(request)) mock_validate.assert_called()
def test_is_authenticated_external_auth_validate_user(self): mock_validate = self.patch(api_auth, 'validate_user_external_auth') mock_validate.return_value = True auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.auth_last_check = (datetime.utcnow() - timedelta(days=1)) mock_token = mock.Mock(user=user) request = self.make_request() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertTrue(auth.is_authenticated(request)) mock_validate.assert_called_with( user, ExternalAuthInfo('candid', 'https://example.com', 'domain', 'admins'))
def test_is_authenticated_external_auth_validate_fail(self): mock_validate = self.patch(api_auth, 'validate_user_external_auth') mock_validate.return_value = False auth = MAASAPIAuthentication() user = factory.make_User() user.userprofile.auth_last_check = (datetime.utcnow() - timedelta(days=1)) mock_token = mock.Mock(user=user) request = self.make_request() auth.is_valid_request = lambda request: True auth.validate_token = lambda request: (mock.Mock(), mock_token, None) self.assertFalse(auth.is_authenticated(request)) # check interval not expired, the user isn't checked mock_validate.assert_called_with( user, ExternalAuthInfo(type='candid', url='https://example.com', domain='domain', admin_group='admins'))
def test_is_authenticated_external_auth(self): user = factory.make_User() request = self.make_request(user=user) auth = MAASAPIAuthentication() self.assertTrue(auth.is_authenticated(request))
def test_is_authenticated(self): Config.objects.set_config("external_auth_url", "") user = factory.make_User() request = self.make_request(user=user) auth = MAASAPIAuthentication() self.assertTrue(auth.is_authenticated(request))
def test_is_authenticated(self): user = factory.make_User() request = factory.make_fake_request('/') request.user = user auth = MAASAPIAuthentication() self.assertTrue(auth.is_authenticated(request))