# 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 shutil import tempfile import unittest import qvisqve class AuthenticatingEntityManagerTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() fs = qvisqve.FileStore(self.tempdir) self.aem = qvisqve.AuthenticatingEntityManager(fs, 'client') def tearDown(self): shutil.rmtree(self.tempdir) def test_does_not_validate_secret_if_entity_does_not_exist(self): self.assertFalse( self.aem.is_valid_secret('does-not-exist', 'whatever')) def test_does_not_validate_secret_if_not_stored(self): secret = 'hunter2' client = { 'id': 'test-client', } self.aem.create(client['id'], client) self.assertFalse(self.aem.is_valid_secret(client['id'], secret)) def test_validates_secret(self): secret = 'hunter2' client = { 'id': 'test-client', } self.aem.create(client['id'], client) self.aem.set_secret(client['id'], secret) self.assertFalse(self.aem.is_valid_secret(client['id'], 'invalid')) self.assertTrue(self.aem.is_valid_secret(client['id'], secret)) def test_returns_empty_list_of_scopes_initially(self): client = { 'id': 'test-client', } self.aem.create(client['id'], client) self.assertEqual(self.aem.get_allowed_scopes(client['id']), []) def test_sets_allowed_scopes(self): client = { 'id': 'test-client', } scopes = ['foo', 'bar'] self.aem.create(client['id'], client) self.aem.set_allowed_scopes(client['id'], scopes) self.assertEqual(self.aem.get_allowed_scopes(client['id']), scopes) class ClientManagerTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() fs = qvisqve.FileStore(self.tempdir) self.cm = qvisqve.ClientManager(fs) def tearDown(self): shutil.rmtree(self.tempdir) def test_validates_client_secret(self): secret = 'hunter2' client = { 'id': 'test-client', } self.cm.create(client['id'], client) self.cm.set_secret(client['id'], secret) self.assertTrue(self.cm.is_valid_secret(client['id'], secret)) def test_has_no_subject_initially(self): client = { 'id': 'test-client', } self.cm.create(client['id'], client) self.assertEqual(self.cm.get_subject(client['id']), None) def test_sets_subject(self): client = { 'id': 'test-client', } self.cm.create(client['id'], client) self.cm.set_subject(client['id'], 'tomjon') self.assertEqual(self.cm.get_subject(client['id']), 'tomjon') class UserManagerTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() fs = qvisqve.FileStore(self.tempdir) self.um = qvisqve.UserManager(fs) def tearDown(self): shutil.rmtree(self.tempdir) def test_creates_user(self): user = { 'id': 'tomjon', } self.um.create(user['id'], user) self.assertEqual(self.um.get(user['id']), user)