from flask import json

from tests.base_test import BaseTest


class TestAuthentication(BaseTest):
    def test_user_registration(self):
        """Test if a user is created using the api"""
        response = self.client.post("/v1/register",
                                    data=json.dumps({"name": "esirick",
                                                     "password": "<PASSWORD>",
                                                     "email": "<EMAIL>"
                                                     }),
                                    content_type='application/json')
        self.assertEqual(response.status_code, 201)

        response_no_arg = self.client.post("/v1/register",
                                           data=json.dumps({"name": "esirick",
                                                            "password": "<PASSWORD>",
                                                            "email": "  "
                                                            }),
                                           content_type='application/json')
        """
        Test if no arguments are passed the service aborts
        """
        self.assertEqual(400, response_no_arg.status_code)

    def test_registration_of_existing_user_fails(self):
        self.client.post("/v1/register",
                         data=json.dumps({"name": "esirick",
                                          "password": "<PASSWORD>",
                                          "email": "<EMAIL>"
                                          }),
                         content_type='application/json')
        response = self.client.post("/v1/register",
                                    data=json.dumps({"name": "esirick",
                                                     "password": "<PASSWORD>",
                                                     "email": "<EMAIL>"
                                                     }),
                                    content_type='application/json')
        self.assertIn(
            "Already Exists", response.data.decode())

    def test_verify_password(self):
        """
        Test if user is authenticated using email and password
        """
        res = self.client.get("/v1/token", headers=self.headers)
        self.assertEqual(res.status_code, 200)

    def test_authentication_with_token(self):
        """
        Test if user is authenticated using a valid token
        """
        res = self.client.get("/v1/token", headers=self.token_headers)
        """Accessing Secure endpoint using a token"""
        self.assertEqual(res.status_code, 200)

    def test_user_login(self):
        """
        Test If A Registered User Can Login to the app using registration
         credentials
        """
        self.client.post(
            "/v1/register",
            data=json.dumps({"name": "esirick",
                             "password": "<PASSWORD>",
                             "email": "<EMAIL>"
                             }),
            content_type='application/json')
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(200, response.status_code)

    def test_user_update(self):
        """Test a logged in user can update their account details"""
        response = self.client.put(
            "/v1/user",
            data=json.dumps({
                "name": "Kamaku",
                "password": "<PASSWORD>"
            }),
            content_type='application/json', headers=self.headers)
        self.assertEqual(200, response.status_code)
        self.assertIn("Kamaku", json.loads(response.data)["User"]["username"])

    def test_user_can_not_login_with_invalid_creds(self):
        """
        Test If A Registered User Can Login to the app using
        Invalid Credentials
         """
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(401, response.status_code)

    def test_un_registered_user_can_not_login(self):
        """
        Test If a Un Registered User Can Login to the app
         """
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(401, response.status_code)

from flask import json

from tests.base_test import BaseTest


class TestAuthentication(BaseTest):
    def test_user_registration(self):
        """Test if a user is created using the api"""
        response = self.client.post("/v1/register",
                                    data=json.dumps({"name": "esirick",
                                                     "password": "<PASSWORD>",
                                                     "email": "<EMAIL>"
                                                     }),
                                    content_type='application/json')
        self.assertEqual(response.status_code, 201)

        response_no_arg = self.client.post("/v1/register",
                                           data=json.dumps({"name": "esirick",
                                                            "password": "<PASSWORD>",
                                                            "email": "  "
                                                            }),
                                           content_type='application/json')
        """
        Test if no arguments are passed the service aborts
        """
        self.assertEqual(400, response_no_arg.status_code)

    def test_registration_of_existing_user_fails(self):
        self.client.post("/v1/register",
                         data=json.dumps({"name": "esirick",
                                          "password": "<PASSWORD>",
                                          "email": "<EMAIL>"
                                          }),
                         content_type='application/json')
        response = self.client.post("/v1/register",
                                    data=json.dumps({"name": "esirick",
                                                     "password": "<PASSWORD>",
                                                     "email": "<EMAIL>"
                                                     }),
                                    content_type='application/json')
        self.assertIn(
            "Already Exists", response.data.decode())

    def test_verify_password(self):
        """
        Test if user is authenticated using email and password
        """
        res = self.client.get("/v1/token", headers=self.headers)
        self.assertEqual(res.status_code, 200)

    def test_authentication_with_token(self):
        """
        Test if user is authenticated using a valid token
        """
        res = self.client.get("/v1/token", headers=self.token_headers)
        """Accessing Secure endpoint using a token"""
        self.assertEqual(res.status_code, 200)

    def test_user_login(self):
        """
        Test If A Registered User Can Login to the app using registration
         credentials
        """
        self.client.post(
            "/v1/register",
            data=json.dumps({"name": "esirick",
                             "password": "<PASSWORD>",
                             "email": "<EMAIL>"
                             }),
            content_type='application/json')
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(200, response.status_code)

    def test_user_update(self):
        """Test a logged in user can update their account details"""
        response = self.client.put(
            "/v1/user",
            data=json.dumps({
                "name": "Kamaku",
                "password": "<PASSWORD>"
            }),
            content_type='application/json', headers=self.headers)
        self.assertEqual(200, response.status_code)
        self.assertIn("Kamaku", json.loads(response.data)["User"]["username"])

    def test_user_can_not_login_with_invalid_creds(self):
        """
        Test If A Registered User Can Login to the app using
        Invalid Credentials
         """
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(401, response.status_code)

    def test_un_registered_user_can_not_login(self):
        """
        Test If a Un Registered User Can Login to the app
         """
        response = self.client.post(
            "/v1/user",
            data=json.dumps({
                "password": "<PASSWORD>",
                "email": "<EMAIL>"
            }),
            content_type='application/json')
        self.assertEqual(401, response.status_code)

Test if a user is created using the api Test if no arguments are passed the service aborts Test if user is authenticated using email and password Test if user is authenticated using a valid token Accessing Secure endpoint using a token Test If A Registered User Can Login to the app using registration          credentials Test a logged in user can update their account details Test If A Registered User Can Login to the app using         Invalid Credentials Test If a Un Registered User Can Login to the app