Example #1
0
class VkAuthCase(unittest.TestCase):

    def setUp(self):
        self.service = AuthenticationService()

    def tearDown(self):
        self.service.credentials.delete_many({})

    def test_init_registration_with_vk(self):
        os.environ["VK_APP_SECRET_KEY"] = "12345"
        vk_id = 42
        vk_concated_string = "blablabla=1&blabla=2"
        sig = md5(vk_concated_string.replace("&", "") + "12345")
        credentials = Credentials()
        credentials.vk_id = vk_id
        result = self.service.authenticate_vk(credentials, vk_concated_string, sig)
        self.assertTrue(isinstance(result, tuple))
        self.assertEqual(1, result[0])
        self.assertEqual(32, len(result[1]))
        token = result[1]

        # Используя токен, полученный при регистрации через вк авторизуемся стандартным путем:
        credentials.token = token
        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(token, auth_result[1])

    def test_vk_auth_after_normal_registartion(self):
        os.environ["VK_APP_SECRET_KEY"] = "12345"
        vk_id = 42
        vk_concated_string = "blablabla=1&blabla=2"
        sig = md5(vk_concated_string.replace("&", "") + "12345")

        # Обычная регистрация, получаем токен:
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.service.verify_phone(credentials, verification_code)
        auth_result = self.service.authenticate(credentials)
        self.assertEqual(1, auth_result[0])
        token = auth_result[1]

        # Используем токен и укажем vk_id:
        new_credentials = Credentials()
        new_credentials.token = token
        new_credentials.vk_id = vk_id
        vk_auth_result = self.service.authenticate_vk(new_credentials, vk_concated_string, sig)
        self.assertTrue(isinstance(vk_auth_result, tuple))
        self.assertEqual(1, vk_auth_result[0])
        self.assertEqual(token, vk_auth_result[1])

        # Теперь можно и без токена:
        new_credentials = Credentials()
        new_credentials.vk_id = vk_id
        vk_auth_result = self.service.authenticate_vk(new_credentials, vk_concated_string, sig)
        self.assertTrue(isinstance(vk_auth_result, tuple))
        self.assertEqual(1, vk_auth_result[0])
        self.assertEqual(32, len(vk_auth_result[1]))
        self.assertNotEqual(token, vk_auth_result[1])

    def test_vk_auth_fail(self):
        os.environ["VK_APP_SECRET_KEY"] = "12345"
        vk_id = 42
        vk_concated_string = "blablabla=1&blabla=2"
        sig = md5(vk_concated_string.replace("&", "") + "12345")
        credentials = Credentials()
        credentials.vk_id = vk_id
        self.assertRaises(
            IncorrectOAuthSignature,
            self.service.authenticate_vk, credentials, vk_concated_string, sig+"1"
        )
Example #2
0
class ChangeCredentialsCase(unittest.TestCase):

    def setUp(self):
        self.service = AuthenticationService()
        self.email_credentials = Credentials()
        self.email_credentials.email = "*****@*****.**"
        self.email_credentials.password = "******"
        register_result = self.service.register(self.email_credentials)
        verification_code = register_result["verification"]["send_code"]
        self.service.verify_email(self.email_credentials, verification_code)
        auth_result = self.service.authenticate(self.email_credentials)
        self.email_account_token = auth_result[1]

        self.phone_credentials = Credentials()
        self.phone_credentials.phone = "+79263435016"
        self.phone_credentials.password = "******"
        register_result = self.service.register(self.phone_credentials)
        verification_code = register_result["verification"]["send_code"]
        self.service.verify_phone(self.phone_credentials, verification_code)
        auth_result = self.service.authenticate(self.phone_credentials)
        self.phone_account_token = auth_result[1]

    def tearDown(self):
        self.service.credentials.delete_many({})

    def test_change_password(self):
        credentials = Credentials()
        credentials.token = self.email_account_token
        self.assertRaises(IncorrectPassword, self.service.set_new_password, credentials, "blabla", "123", "456")
        self.assertRaises(NewPasswordsMismatch, self.service.set_new_password, credentials, "qwerty123", "123", "456")
        change_password_result = self.service.set_new_password(credentials, "qwerty123", "12345", "12345")
        self.assertTrue(isinstance(change_password_result, dict))
        self.assertTrue("new_password" in change_password_result)
        self.assertEqual("email", change_password_result["new_password"]["send_via"])
        self.assertEqual("*****@*****.**", change_password_result["new_password"]["send_address"])
        self.assertEqual("12345", change_password_result["new_password"]["send_password"])

        credentials = Credentials()
        credentials.token = self.phone_account_token
        self.assertRaises(IncorrectPassword, self.service.set_new_password, credentials, "blabla", "123", "456")
        self.assertRaises(NewPasswordsMismatch, self.service.set_new_password, credentials, "qwerty123", "123", "456")
        change_password_result = self.service.set_new_password(credentials, "qwerty123", "12345", "12345")
        self.assertTrue(isinstance(change_password_result, dict))
        self.assertTrue("new_password" in change_password_result)
        self.assertEqual("phone", change_password_result["new_password"]["send_via"])
        self.assertEqual("+79263435016", change_password_result["new_password"]["send_address"])
        self.assertEqual("12345", change_password_result["new_password"]["send_password"])

    def test_set_and_verify_phone_when_email_verified(self):
        result = self.service.set_new_phone(self.email_credentials, "+79164143212")
        verification_code = result["verification"]["send_code"]
        new_phone_credentials = Credentials()
        new_phone_credentials.phone = "+79164143212"
        new_phone_credentials.password = self.email_credentials.password
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(new_phone_credentials))
        self.service.authenticate(self.email_credentials)
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_phone(new_phone_credentials, verification_code))
        self.service.verify_phone(self.email_credentials, verification_code)
        self.service.authenticate(new_phone_credentials)
        self.service.authenticate(self.email_credentials)

    def test_set_and_verify_new_email_when_email_verified(self):
        result = self.service.set_new_email(self.email_credentials, "*****@*****.**")
        verification_code = result["verification"]["send_code"]
        new_email_credentials = Credentials()
        new_email_credentials.email = "*****@*****.**"
        new_email_credentials.password = self.email_credentials.password
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(new_email_credentials))
        self.service.authenticate(self.email_credentials)
        self.service.verify_email(self.email_credentials, verification_code)
        self.service.authenticate(new_email_credentials)
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(self.email_credentials))

    def test_set_and_verify_email_when_phone_verified(self):
        result = self.service.set_new_email(self.phone_credentials, "*****@*****.**")
        verification_code = result["verification"]["send_code"]
        new_email_credentials = Credentials()
        new_email_credentials.email = "*****@*****.**"
        new_email_credentials.password = self.phone_credentials.password
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(new_email_credentials))
        self.service.authenticate(self.phone_credentials)
        self.service.verify_email(self.phone_credentials, verification_code)
        self.service.authenticate(new_email_credentials)
        self.service.authenticate(self.phone_credentials)

    def test_set_and_verify_new_phone_when_phone_verified(self):
        result = self.service.set_new_phone(self.phone_credentials, "+79212225577")
        verification_code = result["verification"]["send_code"]
        new_phone_credentials = Credentials()
        new_phone_credentials.phone = "+79212225577"
        new_phone_credentials.password = self.phone_credentials.password
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(new_phone_credentials))
        self.service.authenticate(self.phone_credentials)
        self.service.verify_phone(self.phone_credentials, verification_code)
        self.service.authenticate(new_phone_credentials)
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(self.phone_credentials))

    def test_fail_to_verify_phone_when_email_verified_doesnt_remove_credentials(self):
        result = self.service.set_new_phone(self.email_credentials, "+79164143212")
        verification_code = result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, self.service.verify_phone, self.email_credentials, "bla")
        self.assertRaises(IncorrectVerificationCode, self.service.verify_phone, self.email_credentials, "bla")
        self.assertRaises(IncorrectVerificationCode, self.service.verify_phone, self.email_credentials, "bla")
        self.assertRaises(IncorrectVerificationCodeFatal, self.service.verify_phone, self.email_credentials, "bla")
        self.assertRaises(NoVerificationProcess, self.service.verify_phone, self.email_credentials, verification_code)

        self.service.authenticate(self.email_credentials)
        result = self.service.set_new_phone(self.email_credentials, "+79164143212")
        verification_code = result["verification"]["send_code"]
        self.service.verify_phone(self.email_credentials, verification_code)

        new_phone_credentials = Credentials()
        new_phone_credentials.phone = "+79164143212"
        new_phone_credentials.password = self.email_credentials.password
        auth_result = self.service.authenticate(new_phone_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
Example #3
0
class RegistrationCase(unittest.TestCase):

    def setUp(self):
        self.service = AuthenticationService()

    def tearDown(self):
        self.service.credentials.delete_many({})

    def test_init_registration_process_with_email(self):
        credentials = Credentials()
        self.assertRaises(IncorrectLogin, lambda: self.service.register(credentials))
        credentials.email = "*****@*****.**"
        self.assertRaises(IncorrectPassword, lambda: self.service.register(credentials))
        credentials.password = "******"
        register_result = self.service.register(credentials)
        self.assertTrue(isinstance(register_result, dict))
        self.assertTrue("verification" in register_result)
        self.assertTrue("send_code" in register_result["verification"])
        self.assertTrue("send_via" in register_result["verification"])
        self.assertTrue("send_address" in register_result["verification"])
        self.assertEqual("email", register_result["verification"]["send_via"])
        self.assertEqual("*****@*****.**", register_result["verification"]["send_address"])
        verification_code = register_result["verification"]["send_code"]

        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(credentials))
        self.assertTrue(self.service.verify_email(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_init_registration_process_with_phone(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"

        register_result = self.service.register(credentials)
        self.assertTrue(isinstance(register_result, dict))
        self.assertTrue("verification" in register_result)
        self.assertTrue("send_code" in register_result["verification"])
        self.assertTrue("send_via" in register_result["verification"])
        self.assertTrue("send_address" in register_result["verification"])
        self.assertEqual("phone", register_result["verification"]["send_via"])
        self.assertEqual("+79263435016", register_result["verification"]["send_address"])
        verification_code = register_result["verification"]["send_code"]

        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(credentials))
        self.assertTrue(self.service.verify_phone(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_fail_email_verification_and_delete_account(self):
        credentials = Credentials()
        credentials.email = "*****@*****.**"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCodeFatal, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_email(credentials, verification_code))

    def test_fail_phone_verification_and_delete_account(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCodeFatal, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_phone(credentials, verification_code))

    def test_email_verification_with_third_attempt(self):
        credentials = Credentials()
        credentials.email = "*****@*****.**"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "blablabla"))
        self.assertTrue(self.service.verify_email(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_phone_verification_with_third_attempt(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "blablabla"))
        self.assertTrue(self.service.verify_phone(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))
Example #4
0
class AuthentificationCase(unittest.TestCase):

    def setUp(self):
        self.service = AuthenticationService()
        self.email_credentials = Credentials()
        self.email_credentials.email = "*****@*****.**"
        self.email_credentials.password = "******"
        register_result = self.service.register(self.email_credentials)
        verification_code = register_result["verification"]["send_code"]
        self.service.verify_email(self.email_credentials, verification_code)

        self.phone_credentials = Credentials()
        self.phone_credentials.phone = "+79263435016"
        self.phone_credentials.password = "******"
        register_result = self.service.register(self.phone_credentials)
        verification_code = register_result["verification"]["send_code"]
        self.service.verify_phone(self.phone_credentials, verification_code)

    def tearDown(self):
        self.service.credentials.delete_many({})

    def test_successfull_auth_with_login_and_password(self):
        auth_result = self.service.authenticate(self.email_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

        auth_result = self.service.authenticate(self.phone_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(2, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_wrong_login_and_then_success(self):
        self.email_credentials.email = "*****@*****.**"
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(self.email_credentials))
        self.email_credentials.email = "*****@*****.**"
        auth_result = self.service.authenticate(self.email_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

        self.phone_credentials.phone = "911"
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(self.phone_credentials))
        self.phone_credentials.phone = "+79263435016"
        auth_result = self.service.authenticate(self.phone_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(2, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_wrong_password_and_then_success(self):
        self.email_credentials.password = "******"
        self.assertRaises(IncorrectPassword, lambda: self.service.authenticate(self.email_credentials))
        self.email_credentials.password = "******"
        auth_result = self.service.authenticate(self.email_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

        self.phone_credentials.password = "******"
        self.assertRaises(IncorrectPassword, lambda: self.service.authenticate(self.phone_credentials))
        self.phone_credentials.password = "******"
        auth_result = self.service.authenticate(self.phone_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(2, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_password_recovery(self):
        self.email_credentials.password = "******"
        self.assertRaises(IncorrectPassword, lambda: self.service.authenticate(self.email_credentials))
        recover_result = self.service.recover_password(self.email_credentials)
        self.assertTrue(isinstance(recover_result, dict))
        self.assertEqual("email", recover_result["password_recovery"]["send_via"])
        self.assertEqual("*****@*****.**", recover_result["password_recovery"]["send_address"])
        self.assertTrue(isinstance(recover_result["password_recovery"]["send_password"], str))

        self.email_credentials.password = recover_result["password_recovery"]["send_password"]
        auth_result = self.service.authenticate(self.email_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

        self.phone_credentials.password = "******"
        self.assertRaises(IncorrectPassword, lambda: self.service.authenticate(self.phone_credentials))
        recover_result = self.service.recover_password(self.phone_credentials)
        self.assertTrue(isinstance(recover_result, dict))
        self.assertEqual("phone", recover_result["password_recovery"]["send_via"])
        self.assertEqual("+79263435016", recover_result["password_recovery"]["send_address"])
        self.assertTrue(isinstance(recover_result["password_recovery"]["send_password"], str))

        self.phone_credentials.password = recover_result["password_recovery"]["send_password"]
        auth_result = self.service.authenticate(self.phone_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(2, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))

    def test_auth_by_token(self):
        auth_result = self.service.authenticate(self.email_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(32, len(auth_result[1]))
        token = auth_result[1]

        new_credentials = Credentials()
        new_credentials.token = token
        auth_result = self.service.authenticate(new_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(token, token)
        # При авторизации по токену, токен не меняется:
        auth_result = self.service.authenticate(new_credentials)
        self.assertTrue(isinstance(auth_result, tuple))
        self.assertEqual(1, auth_result[0])
        self.assertEqual(token, token)
Example #5
0
class RegistrationCase(unittest.TestCase):

    def setUp(self):
        self.service = AuthenticationService()

    def tearDown(self):
        self.service.credentials.delete_many({})

    def test_init_registration_process_with_email(self):
        credentials = Credentials()
        self.assertRaises(IncorrectLogin, lambda: self.service.register(credentials))
        credentials.email = "*****@*****.**"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        self.assertTrue(isinstance(register_result, dict))
        self.assertTrue("verification" in register_result)
        self.assertTrue("send_code" in register_result["verification"])
        self.assertTrue("send_via" in register_result["verification"])
        self.assertTrue("send_address" in register_result["verification"])
        self.assertEqual("email", register_result["verification"]["send_via"])
        self.assertEqual("*****@*****.**", register_result["verification"]["send_address"])
        verification_code = register_result["verification"]["send_code"]

        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(credentials))
        self.assertTrue(self.service.verify_email(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))

    def test_init_registration_process_with_phone(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"

        register_result = self.service.register(credentials)
        self.assertTrue(isinstance(register_result, dict))
        self.assertTrue("verification" in register_result)
        self.assertTrue("send_code" in register_result["verification"])
        self.assertTrue("send_via" in register_result["verification"])
        self.assertTrue("send_address" in register_result["verification"])
        self.assertEqual("phone", register_result["verification"]["send_via"])
        self.assertEqual("+79263435016", register_result["verification"]["send_address"])
        verification_code = register_result["verification"]["send_code"]

        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(credentials))
        self.assertTrue(self.service.verify_phone(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))

    def test_fail_email_verification_and_delete_account(self):
        credentials = Credentials()
        credentials.email = "*****@*****.**"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCodeFatal, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_email(credentials, verification_code))

    def test_fail_phone_verification_and_delete_account(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCodeFatal, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectLogin, lambda: self.service.verify_phone(credentials, verification_code))

    def test_email_verification_with_third_attempt(self):
        credentials = Credentials()
        credentials.email = "*****@*****.**"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_email(credentials, "9999"))
        self.assertTrue(self.service.verify_email(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))

    def test_phone_verification_with_third_attempt(self):
        credentials = Credentials()
        credentials.phone = "+79263435016"
        credentials.password = "******"
        register_result = self.service.register(credentials)
        verification_code = register_result["verification"]["send_code"]
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertRaises(IncorrectVerificationCode, lambda: self.service.verify_phone(credentials, "9999"))
        self.assertTrue(self.service.verify_phone(credentials, verification_code))

        auth_result = self.service.authenticate(credentials)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))

    def test_second_attempt_to_start_registration_process(self):
        credentials1 = Credentials()
        credentials1.email = "*****@*****.**"
        credentials1.password = "******"
        register_result1 = self.service.register(credentials1)
        verification_code1 = register_result1["verification"]["send_code"]

        credentials2 = Credentials()
        credentials2.email = "*****@*****.**"
        credentials2.password = "******"
        register_result2 = self.service.register(credentials2)
        verification_code2 = register_result2["verification"]["send_code"]

        self.assertNotEqual(verification_code1, verification_code2)
        self.assertTrue(self.service.verify_email(credentials2, verification_code2))
        auth_result = self.service.authenticate(credentials2)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))

    def test_registration_with_autogenerated_password(self):
        credentials = Credentials()
        self.assertRaises(IncorrectLogin, lambda: self.service.register(credentials))
        credentials.email = "*****@*****.**"
        register_result = self.service.register(credentials)
        self.assertTrue(isinstance(register_result, dict))
        self.assertTrue("verification" in register_result)
        self.assertTrue("id" in register_result["verification"])
        self.assertTrue("password" in register_result["verification"])
        self.assertTrue("send_code" in register_result["verification"])
        self.assertTrue("send_via" in register_result["verification"])
        self.assertTrue("send_address" in register_result["verification"])
        self.assertEqual(1, register_result["verification"]["id"])
        self.assertEqual(8, len(register_result["verification"]["password"]))
        self.assertEqual("email", register_result["verification"]["send_via"])
        self.assertEqual("*****@*****.**", register_result["verification"]["send_address"])
        verification_code = register_result["verification"]["send_code"]

        credentials2 = Credentials()
        credentials2.email = "*****@*****.**"
        self.assertRaises(IncorrectLogin, lambda: self.service.authenticate(credentials2))
        verification_result = self.service.verify_email(credentials2, verification_code)
        self.assertTrue(isinstance(verification_result, dict))
        self.assertTrue("verification" in verification_result)
        self.assertTrue("result" in verification_result["verification"])
        self.assertEqual(True, verification_result["verification"]["result"])
        self.assertTrue("password" in verification_result["verification"])
        self.assertEqual(8, len(verification_result["verification"]["password"]))

        self.assertRaises(IncorrectPassword, lambda : self.service.authenticate(credentials2))
        credentials2.password = verification_result["verification"]["password"]
        auth_result = self.service.authenticate(credentials2)
        self.assertTrue(isinstance(auth_result, dict))
        self.assertEqual(1, auth_result["authentication"]["id"])
        self.assertEqual(32, len(auth_result["authentication"]["token"]))