summaryrefslogtreecommitdiff
path: root/qvisqve/authz_attempt_tests.py
blob: d46660daa27c49293ee580ab8723ec2bc14b38a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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 <http://www.gnu.org/licenses/>.


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_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)