class AuthManagerBase(unittest.TestCase): klass = None def setUp(self): self.prefix = tempfile.mkdtemp() self.config = Configuration() self.config.update(use_webservice=False, indexed_repositories=["http://acme.com"]) self.session = Session(self.klass.from_configuration(self.config), self.prefix) def tearDown(self): shutil.rmtree(self.prefix) self.session.close()
class CheckedChangeAuthTestCase(unittest.TestCase): def setUp(self): self.d = tempfile.mkdtemp() self.f = os.path.join(self.d, "enstaller4rc") self.session = Session(DummyAuthenticator(), self.d) def tearDown(self): self.session.close() shutil.rmtree(self.d) @responses.activate def test_no_acct(self): # Given url = "https://acme.com" auth_url = url + "/accounts/user/info/" config = Configuration() config.update(store_url=url) def callback(request): if auth != ("valid_user", "valid_password"): return (403, {}, "") return (200, {}, json.dumps(R_JSON_AUTH_RESP)) responses.add_callback(responses.GET, auth_url, callback) write_default_config(self.f) with Session.from_configuration(config) as session: config = Configuration() config.update(store_url=url) auth = ("invalid_user", "invalid_password") with self.assertRaises(AuthFailedError): usr = config._checked_change_auth(auth, session, self.f) config = Configuration() auth = ("valid_user", "valid_password") usr = config._checked_change_auth(auth, session, self.f) self.assertTrue(usr.is_authenticated) self.assertEqual(config.auth, UserPasswordAuth("valid_user", "valid_password")) def test_remote_success(self): write_default_config(self.f) config = Configuration() auth = ("usr", "password") session = Session(DummyAuthenticator(old_auth_user), self.d) with session: usr = config._checked_change_auth(auth, session, self.f) self.assertEqual(usr, UserInfo(True)) def test_nones(self): config = Configuration() with self.assertRaises(InvalidConfiguration): config.update(auth=(None, None))