def test_expired_consent(self): """A consent request which has expired should raise ConsentError""" consent = get_valid_consent() consent['expiresAt'] = (datetime.datetime.now(tz.tzutc()) - datetime.timedelta(hours=1)).isoformat() with self.assertRaises(hydra.ConsentError): hydra.verify_consent(consent, consent['id'])
def test_login_required(self): """A valid consent requires login.""" consent = get_valid_consent() rav_patch = mock.patch( 'ravenconsent.hydra.retrieve_and_verify_consent') with rav_patch as retrieve_and_verify_consent: retrieve_and_verify_consent.side_effect = lambda _: consent r = self.client.get(self.endpoint + '?consent=' + consent['id']) self.assertEqual(r.status_code, 302) self.assertTrue(r['Location'].startswith(settings.LOGIN_URL))
def test_validate_decision(self): """Passing an invalid value for decision raises ValueError.""" request = self.factory.get(self.endpoint) request.user = self.user consent = get_valid_consent() with self.settings(HYDRA_CONSENT_REQUESTS_ENDPOINT='http://hydra.invalid/', SUBJECT_SCHEME='test-scheme'), \ mock.patch('ravenconsent.hydra._request'), self.assertRaises(ValueError): hydra.resolve_request(request, consent, 'accept', ['a', 'b'])
def test_invalid_consent(self): rav_patch = mock.patch( 'ravenconsent.hydra.retrieve_and_verify_consent') consent = get_valid_consent() def fail(): raise RuntimeError("I don't like your auth") with rav_patch as retrieve_and_verify_consent: retrieve_and_verify_consent.side_effect = fail r = self.client.get(self.endpoint + '?consent=' + consent['id']) self.assertEqual(r.status_code, 400)
def test_successful_flow(self): rav_patch = mock.patch( 'ravenconsent.hydra.retrieve_and_verify_consent') rr_patch = mock.patch('ravenconsent.hydra.resolve_request') consent = get_valid_consent() with rav_patch as retrieve_and_verify_consent, rr_patch as resolve_request: retrieve_and_verify_consent.side_effect = lambda _: consent resolve_request.return_value = HttpResponseRedirect( 'http://test.invalid/') r = self.client.get(self.endpoint + '?consent=' + consent['id']) # Check redirect was passed self.assertEqual(r.status_code, 302) # Check call to resolve_request was made self.assertEqual(len(resolve_request.mock_calls), 1) # Check grants and decision args, kwargs = resolve_request.call_args self.assertEqual(args[2], hydra.Decision.ACCEPT) self.assertEqual(kwargs['grant_scopes'], consent['requestedScopes'])
def test_resolve_reject(self): request = self.factory.get(self.endpoint) request.user = self.user consent = get_valid_consent() with self.settings(HYDRA_CONSENT_REQUESTS_ENDPOINT='http://hydra.invalid/', SUBJECT_SCHEME='test-scheme'), \ mock.patch('ravenconsent.hydra._request') as hydra_request: hydra.resolve_request(request, consent, hydra.Decision.REJECT, ['a', 'b']) # The return from _request should have been checked for HTTP failure hydra_request.return_value.raise_for_status.assert_called_once_with() # Check arguments to _request call _, kwargs = hydra_request.call_args self.assertEqual(kwargs['method'], 'PATCH') self.assertEqual(kwargs['url'], 'http://hydra.invalid/' + consent['id'] + '/reject') self.assertIn('reason', kwargs['json'])
def test_prompt_none(self): """A valid consent with CONSENT_PROMPT_NONE_SCOPE scope redirects to a deny response.""" consent = get_valid_consent( scopes=['a', 'b', settings.CONSENT_PROMPT_NONE_SCOPE]) rav_patch = mock.patch( 'ravenconsent.hydra.retrieve_and_verify_consent') rr_patch = mock.patch('ravenconsent.hydra.resolve_request') with rav_patch as retrieve_and_verify_consent, rr_patch as resolve_request: retrieve_and_verify_consent.side_effect = lambda _: consent resolve_request.return_value = HttpResponseRedirect( 'http://test.invalid/') r = self.client.get(self.endpoint + '?consent=' + consent['id']) self.assertEqual(r.status_code, 302) self.assertEqual(r['Location'], 'http://test.invalid/') # A decision should've been made and that should be a reject resolve_request.assert_called() decision = resolve_request.call_args[0][2] self.assertIs(decision, hydra.Decision.REJECT)
def test_mismatched_id(self): """A consent request with mismatched id should raise ConsentError""" consent = get_valid_consent() with self.assertRaises(hydra.ConsentError): hydra.verify_consent(consent, consent['id'] + '-with-some-junk')
def test_good_consent_validates(self): """A good consent request will validate without throwing.""" consent = get_valid_consent() hydra.verify_consent(consent, consent['id'])
def test_prompt_none(self): """A valid consent with CONSENT_PROMPT_NONE_SCOPE scope succeeds.""" self._assert_consent_successful( get_valid_consent(['a', 'b', settings.CONSENT_PROMPT_NONE_SCOPE]))
def test_successful_flow(self): self._assert_consent_successful(get_valid_consent())