# Copyright (C) 2018 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import unittest import qvisqve class AuthorizationAttemptTests(unittest.TestCase): def test_raises_error_creating_attempt_id_before_all_fields_set(self): gen = qvisqve.NonceGenerator() attempt_id = gen.create_nonce() aa = qvisqve.AuthorizationAttempt() subject_id = 'subject_id' client_id = 'client_id' state = 'state' uri = 'https://facade/callback' scope = 'scope' with self.assertRaises(qvisqve.AuthorizationAttemptError): aa.set_attempt_id(attempt_id) aa.set_client_id(client_id) with self.assertRaises(qvisqve.AuthorizationAttemptError): aa.set_attempt_id(attempt_id) aa.set_state(state) with self.assertRaises(qvisqve.AuthorizationAttemptError): aa.set_attempt_id(attempt_id) aa.set_redirect_uri(uri) with self.assertRaises(qvisqve.AuthorizationAttemptError): aa.set_attempt_id(attempt_id) aa.set_scope(scope) aa.set_subject_id(subject_id) aa.set_attempt_id(attempt_id) self.assertEqual(aa.get_subject_id(), subject_id) self.assertEqual(aa.get_client_id(), client_id) self.assertEqual(aa.get_state(), state) self.assertEqual(aa.get_redirect_uri(), uri) self.assertEqual(aa.get_scope(), scope) self.assertEqual(aa.get_attempt_id(), attempt_id) def test_has_not_authz_code_initially(self): aa = qvisqve.AuthorizationAttempt() self.assertEqual(aa.get_authorization_code(), None) def test_sets_authz_code(self): aa = qvisqve.AuthorizationAttempt() code = '12765' aa.set_authorization_code(code) self.assertEqual(aa.get_authorization_code(), code) class AuthorizationAttemptsTests(unittest.TestCase): def setUp(self): self.urlparams = { 'scope': 'openid read', 'client_id': 'client_id', 'state': 'RANDOM', 'redirect_uri': 'https://facade', } self.aas = qvisqve.AuthorizationAttempts() def test_creates_attempt(self): aa = self.aas.create_attempt(self.urlparams) attempt_id = aa.get_attempt_id() self.assertNotEqual(attempt_id, None) self.assertEqual(aa.get_scope(), self.urlparams['scope']) self.assertEqual(aa.get_client_id(), self.urlparams['client_id']) self.assertEqual(aa.get_state(), self.urlparams['state']) self.assertEqual(aa.get_redirect_uri(), self.urlparams['redirect_uri']) def test_deletes_attempt(self): aa = self.aas.create_attempt(self.urlparams) attempt_id = aa.get_attempt_id() self.aas.delete_by_id(attempt_id) self.assertEqual(self.aas.find_by_id(attempt_id), None) def test_finds_by_id(self): aa = self.aas.create_attempt(self.urlparams) attempt_id = aa.get_attempt_id() self.assertEqual(aa, self.aas.find_by_id(attempt_id)) def test_returns_none_when_finding_by_a_non_existent_id(self): aa = self.aas.create_attempt(self.urlparams) attempt_id = aa.get_attempt_id() nonexistent = attempt_id * 2 self.assertEqual(self.aas.find_by_id(nonexistent), None) def test_finds_by_code(self): aa = self.aas.create_attempt(self.urlparams) code = 'xxx' aa.set_authorization_code(code) self.assertEqual(aa, self.aas.find_by_code(code)) def test_returns_none_when_finding_by_a_non_existent_code(self): aa = self.aas.create_attempt(self.urlparams) code = 'xxx' aa.set_authorization_code(code) nonexistent = 'yyy' self.assertEqual(self.aas.find_by_code(nonexistent), None)