<reponame>Eyansky/Maintenance-Tracker-App<filename>myapi/tests/test_users.py
"""
Main Test  for users
"""

import json
import unittest
from myapi.api import app


class TestUsers(unittest.TestCase):
    """
        Test Users
    """

    def setUp(self):
        """
        code that is executed before each test
        """
        app.testing = True
        self.app = app.test_client()
        self.data = {
            "firstname": "Ian",
            "lastname": "Mwangi",
            "email": "<EMAIL>",
            "username": "eyansky",
            "password": "<PASSWORD>"
        }

        self.error_data = {
            "firstname": "mwangi",
            "lastname": "wairimu",
            "email": "<EMAIL>",
            "username": "mwangi",
            "password": "x"
        }

        self.user = {
            "username": "wairimu",
            "password": "<PASSWORD>"
        }

        self.error_user = {
            "username": "wairimu",
            "password": "<PASSWORD>"
        }

    def test_register(self):
        """
        Test for successful registration
        """
        response = self.app.post('api/v1/users/register',
                                 data=json.dumps(self.data),
                                 content_type="application/json")
        result = json.loads(response.data)
        self.assertEqual(result["status"], "ok")
        self.assertEqual(response.status_code, 201)

    def test_view_all_users(self):
        """
        Test for view all users
        """
        response = self.app.get('api/v1/users/register',
                                data=json.dumps(self.data),
                                content_type="application/json")
        self.assertEqual(response.status_code, 200)

    def test_login(self):
        """
        Test for successful Login
        """
        response = self.app.post('api/v1/users/login',
                                 data=json.dumps(self.user),
                                 content_type="application/json")
        self.assertEqual(response.status_code, 200)

    def test_errorlogin(self):
        """
        Test for invalid Login
        """
        response = self.app.post('api/v1/users/login',
                                 data=json.dumps(self.error_user),
                                 content_type="application/json")
        self.assertEqual(response.status_code, 400)


if __name__ == "__main__":
    unittest.main()

"""
Main Test  for users
"""

import json
import unittest
from myapi.api import app


class TestUsers(unittest.TestCase):
    """
        Test Users
    """

    def setUp(self):
        """
        code that is executed before each test
        """
        app.testing = True
        self.app = app.test_client()
        self.data = {
            "firstname": "Ian",
            "lastname": "Mwangi",
            "email": "<EMAIL>",
            "username": "eyansky",
            "password": "<PASSWORD>"
        }

        self.error_data = {
            "firstname": "mwangi",
            "lastname": "wairimu",
            "email": "<EMAIL>",
            "username": "mwangi",
            "password": "x"
        }

        self.user = {
            "username": "wairimu",
            "password": "<PASSWORD>"
        }

        self.error_user = {
            "username": "wairimu",
            "password": "<PASSWORD>"
        }

    def test_register(self):
        """
        Test for successful registration
        """
        response = self.app.post('api/v1/users/register',
                                 data=json.dumps(self.data),
                                 content_type="application/json")
        result = json.loads(response.data)
        self.assertEqual(result["status"], "ok")
        self.assertEqual(response.status_code, 201)

    def test_view_all_users(self):
        """
        Test for view all users
        """
        response = self.app.get('api/v1/users/register',
                                data=json.dumps(self.data),
                                content_type="application/json")
        self.assertEqual(response.status_code, 200)

    def test_login(self):
        """
        Test for successful Login
        """
        response = self.app.post('api/v1/users/login',
                                 data=json.dumps(self.user),
                                 content_type="application/json")
        self.assertEqual(response.status_code, 200)

    def test_errorlogin(self):
        """
        Test for invalid Login
        """
        response = self.app.post('api/v1/users/login',
                                 data=json.dumps(self.error_user),
                                 content_type="application/json")
        self.assertEqual(response.status_code, 400)


if __name__ == "__main__":
    unittest.main()
Main Test  for users Test Users code that is executed before each test Test for successful registration Test for view all users Test for successful Login Test for invalid Login