def test_authentication_change_password_fail(self):
     fake_persist = FakePersist()
     fake_persist.custom_retrieve_value = {}
     auth = Authentication(fake_persist)
     auth.add_user('foobar', 'baz', 'student')
     self.assertDictEqual(auth.change_password('foobar', 'qux', 'wobble'),
                          {})
class RequestAccessControlTests(unittest.TestCase):

    def setUp(self):
        self.auth = Authentication(EphemeralAuthenticationBackend())

    def test_sequential_api_calls_call_backend(self):
        self.assertTrue(RequestAccessControl(self.auth).enforce_sequential_api_calls(lambda role: True))
        self.assertTrue(RequestAccessControl(self.auth).enforce_sequential_api_calls(lambda role: True))

    def test_second_parallel_api_call_is_processed_sequentially(self):
        access_controller = RequestAccessControl(self.auth)
        first_api_call = threading.Thread(target=lambda: access_controller.enforce_sequential_api_calls(lambda role: time.sleep(1)))
        first_api_call.start()

        second_response = access_controller.enforce_sequential_api_calls(lambda role: True)
        self.assertTrue(second_response)

        first_api_call.join()
        self.assertTrue(RequestAccessControl(self.auth).enforce_sequential_api_calls(lambda role: True))

    def test_user_with_valid_role_hits_endpoint(self):
        self.assertTrue(RequestAccessControl(self.auth).enforce_sequential_api_calls(lambda role: True, "user"))

    def capture_role(self, role):
        self.captured_role = role

    def test_capturing_user_role_on_added_user(self):
        self.assertEqual(('', 200), self.auth.add_user("testRoleUser",
                                                                               {"password": base64.b64encode("password".encode()), "role": "support"},
                                                                               "admin"))
        RequestAccessControl(self.auth).enforce_sequential_api_calls(lambda role: self.capture_role(role), "testRoleUser")
        self.assertEqual("support", self.captured_role)
 def test_authentication_change_password(self):
     auth = Authentication(FakePersist())
     auth.add_user('foobar', 'baz', 'student')
     auth.login('foobar', 'baz')
     self.assertDictEqual(auth.change_password('foobar', 'qux'), {})
 def test_authentication_login_wrong_password_fail(self):
     auth = Authentication(FakePersist())
     auth.add_user('wobble', 'spam', 'student')
     self.assertDictEqual(auth.login('wobble', 'wrong_password'), {})
 def test_authentication_login(self):
     auth = Authentication(FakePersist())
     auth.add_user('foobar', 'baz', 'professor')
     self.assertDictEqual(auth.login('foobar', 'baz'), {})
 def test_authentication_add_user_twice_fail(self):
     auth = Authentication(FakePersist())
     auth.add_user('foobar', 'baz', 'student')
     self.assertDictEqual(auth.add_user('foobar', 'baz', 'student'), {})
 def test_authentication_add_user(self):
     fake_persist = FakePersist()
     auth = Authentication(fake_persist)
     auth.add_user('foobar', 'baz', 'professor')
     self.assertIsInstance(fake_persist.structure, User)