def test_confirm_api_invalid_token(self):
        # Check response when token is invalid
        request_dict = {
            "email": "*****@*****.**",
            "username": "******",
            "password": "******"
        }
        request_data = json.dumps(request_dict)
        response = self.client.post(url_for("api_auth_blueprint.register"),
                                    data=request_data)
        self.assertEqual(response.status_code, 200)

        data = response.get_data(as_text=True)
        self.assertIsNotNone(data)
        data = json.loads(data)

        token = data["token"]
        registered_user = db_util.get_user(data["id"])
        request_data = json.dumps(request_dict)
        response = self.client.post(url_for("api_auth_blueprint.confirm",
                                            token="1234"),
                                    data=request_data)
        self.assertEqual(response.status_code, 401)

        data = response.get_data(as_text=True)
        self.assertIsNotNone(data)
        data = json.loads(data)
        self.assertIn("Error", data)
        self.assertEqual(data["Error"], "The token specified is invalid.")
        self.assertFalse(registered_user.confirmed)
    def test_confirm_api_confirm_registration(self):
        # Check response when successful confirmation is done.
        request_dict = {
            "email": "*****@*****.**",
            "username": "******",
            "password": "******"
        }
        request_data = json.dumps(request_dict)
        response = self.client.post(url_for("api_auth_blueprint.register"),
                                    data=request_data)
        self.assertEqual(response.status_code, 200)

        data = response.get_data(as_text=True)
        self.assertIsNotNone(data)
        data = json.loads(data)

        token = data["token"]
        registered_user = db_util.get_user(data["id"])
        self.assertFalse(registered_user.confirmed)

        request_data = json.dumps(request_dict)
        response = self.client.post(url_for("api_auth_blueprint.confirm",
                                            token=token),
                                    data=request_data)
        self.assertEqual(response.status_code, 200)

        data = response.get_data(as_text=True)
        self.assertIsNotNone(data)
        data = json.loads(data)
        self.assertIn("message", data)
        self.assertEqual(data["message"],
                         "You have successfully verified your account.")

        self.assertTrue(registered_user.confirmed)
    def test_get_user(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        registered_user = register(user_json)

        user = db_util.get_user(registered_user.id)
        self.assertEqual(registered_user.id, user.id)
        self.assertEqual(test_user.email, user.email)
        self.assertEqual(test_user.username, user.username)
        self.assertEqual(registered_user.password_hash, user.password_hash)
Beispiel #4
0
    def register_and_confirm(test_obj, user_dict):
        # Helper function to register a user AND confirm the user.
        data = UserAPITestCase.register_user(test_obj, user_dict)
        registered_user = db_util.get_user(data["id"])
        token = data["token"]

        response = test_obj.client.post(url_for("api_auth_blueprint.confirm",
                                                token=token),
                                        data=json.dumps(user_dict))
        test_obj.assertEqual(response.status_code, 200)

        data = response.get_data(as_text=True)
        test_obj.assertIsNotNone(data)
        data = json.loads(data)
        test_obj.assertIn("message", data)
        test_obj.assertEqual(data["message"],
                             "You have successfully verified your account.")
        test_obj.assertTrue(registered_user.confirmed)

        return registered_user
Beispiel #5
0
    def test_update_user_email(self):
        # Test udpate_user with only email
        user_data = self.get_user_data(0)
        user_dict = self.get_user_dict(user_data)
        registered_user_dict = UserAPITestCase.register_user(self, user_dict)
        registered_user = db_util.get_user(registered_user_dict["id"])
        confirmed = registered_user.confirmed

        # Update user.
        user_dict["email"] = "Updated email"
        response = self.client.put(url_for("api_blueprint.update_user",
                                           user_id=registered_user.id),
                                   data=json.dumps(user_dict))
        self.assertEqual(response.status_code, 200)
        data = response.get_data()
        self.assertIsNotNone(data)
        data = json.loads(data)
        self.assertEqual(user_data.username, data["username"])
        self.assertEqual(confirmed, data["confirmed"])
        self.assertEqual(user_dict["email"], data["email"])
        self.assertNotEqual(user_data.email, data["email"])
Beispiel #6
0
    def register_user(test_obj, user_dict):
        # Helper function to register a user.
        response = test_obj.client.post(url_for("api_auth_blueprint.register"),
                                        data=json.dumps(user_dict))
        test_obj.assertEqual(response.status_code, 200)

        data = response.get_data(as_text=True)
        test_obj.assertIsNotNone(data)
        data = json.loads(data)
        for key in ("id", "email", "username", "url", "message", "token"):
            test_obj.assertIn(key, data)

        test_obj.assertEqual(data["username"], user_dict["username"])
        test_obj.assertEqual(data["email"], user_dict["email"])
        test_obj.assertEqual(
            data["message"],
            "To confirm your account, send a POST request to the given url, with your email and password."
        )

        registered_user = db_util.get_user(data["id"])
        test_obj.assertFalse(registered_user.confirmed)

        return data