def setUp(self): self.user = MailUser(internal_username=INTERNAL_USERNAME, internal_password=CORRECT_INTERNAL_PASSWORD, external_username=EXTERNAL_USERNAME, external_password=EXTERNAL_PASSWORD, server=mail_server, auth_method="noSuchMethod") self.user.save()
class AuthMethodsTest(TestCase): def setUp(self): self.user = MailUser(internal_username=INTERNAL_USERNAME, internal_password=CORRECT_INTERNAL_PASSWORD, external_username=EXTERNAL_USERNAME, external_password=EXTERNAL_PASSWORD, server=mail_server, auth_method="noSuchMethod") self.user.save() def test_no_such_method(self): self.assertRaises(KeyError, getattr, self.user, "auth_class") self.assertRaises(KeyError, self.user.authenticate, CORRECT_INTERNAL_PASSWORD) self.assertRaises(KeyError, self.user.change_password, "some pass") def test_base_method(self): self.user.auth_method = "BaseAuthenticationMethod" self.user.save() self.assertIs(self.user.auth_class, methods.BaseAuthenticationMethod) self.assertFalse(self.user.authenticate(CORRECT_INTERNAL_PASSWORD)) # The password cannot be Null self.assertRaises(IntegrityError, self.user.change_password, u"Some new password") def test_plain_method(self): self.user.auth_method = "PlaintextAuthenticationMethod" self.user.save() self.assertIs(self.user.auth_class, methods.PlaintextAuthenticationMethod) self.assertTrue(self.user.authenticate(CORRECT_INTERNAL_PASSWORD)) self.user.change_password(CORRECT_INTERNAL_PASSWORD) self.assertEqual(self.user.internal_password, CORRECT_INTERNAL_PASSWORD) def test_md5_method(self): self.user.auth_method = "MD5AuthenticationMethod" self.user.save() self.assertIs(self.user.auth_class, methods.MD5AuthenticationMethod) md5_pwd = u'32250170a0dca92d53ec9624f336ca24' self.user.internal_password = md5_pwd self.user.save() self.assertTrue(self.user.authenticate(CORRECT_INTERNAL_PASSWORD)) self.user.change_password(CORRECT_INTERNAL_PASSWORD) self.assertEqual(self.user.internal_password, md5_pwd) def test_md5_name_hostname_pwd_method(self): self.user.auth_method = "MD5NameHostnamePwdAuthenticationMethod" self.user.save() self.assertIs(self.user.auth_class, methods.MD5NameHostnamePwdAuthenticationMethod) md5_name_hostname_pwd = u'99128b41e0484eef1628332c235f0b01' self.user.internal_password = md5_name_hostname_pwd self.user.save() self.assertTrue(self.user.authenticate(CORRECT_INTERNAL_PASSWORD)) self.user.change_password(CORRECT_INTERNAL_PASSWORD) self.assertEqual(self.user.internal_password, md5_name_hostname_pwd) def tearDown(self): self.user.delete()