<gh_stars>0
import unittest
import sys
import requests
import pytest
from app import create_app
from flask import json
from app.dbconn import drop_tables,create_tables


class UserTestCase(unittest.TestCase):
    def setUp(self):
        self.app=create_app("testing")
        self.client=self.app.test_client()
        self.context = self.app.app_context()
        with self.context:
            create_tables()
            
        self.logged_in_admin={"email":'<EMAIL>',"password":'<PASSWORD>'}
        self.register_user_without_email ={ "username":"joan", "password":"<PASSWORD>","role":"User"}
        self.register_user = { "username":"joan","email": "<EMAIL>", "password":"<PASSWORD>","role":"User"}
        self.register_invalid_email = { "username":"joan","email": "<EMAIL>", "password":"<PASSWORD>","role":"User"}
        self.login_user = {  "email":'<EMAIL>',"password":'<PASSWORD>' }
        self.login_user_empty_email= { "email": "", "password":"<PASSWORD>" }
        self.login_user_empty_password= { "email": "<EMAIL>", "password":""}
        self.login_empty_email_password={"email":"","password":""}
        self.test_user7 ={"email":'<EMAIL>',"password":'<PASSWORD>' }
        self.login_normal_user={"email":"<EMAIL>","password":"<PASSWORD>"}
        self.test_register_empty_role ={"username":"joan","email": "<EMAIL>", "password":"<PASSWORD>","role":""}
        self.register_user_existing = {"username":"higi","email": "<EMAIL>", "password":"<PASSWORD>","role":"Admin"}


    


    def test_login(self):
        # REGISTER ADMIN
        
        response = self.client.post('/api/v2/users/login',
         data = json.dumps(dict(self.login_user)),
         content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"User was logged in succesfully")
        self.assertEqual(response.status_code, 200)
    
    def test_empty_password(self):
        response = self.client.post('/api/v2/users/login',
         data = json.dumps(dict(self.login_user_empty_password)),
         content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"password cannot be empty")
        self.assertEqual(response.status_code, 400)
    def test_empty_email(self):
        response = self.client.post('/api/v2/users/login',
            data = json.dumps(dict(self.login_user_empty_email)),
            content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"email can not be empty")
        self.assertEqual(response.status_code, 400)
    def test_login_empty_email_password(self):
        response = self.client.post('/api/v2/users/login',
            data = json.dumps(dict(self.login_empty_email_password)),
            content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"email can not be empty")
        self.assertEqual(response.status_code, 400)

    def test_register_empty_role(self):
        response = self.client.post(
        '/api/v2/users/login',
         data = json.dumps(dict(
            self.logged_in_admin)),
         content_type = 'application/json')
        response_data = json.loads(response.data.decode())

        token = json.loads(response.data.decode())['access_token']

        response = self.client.post('/api/v2/auth/user',
        data = json.dumps(self.test_register_empty_role),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"All fields are required")
        self.assertEqual(response.status_code, 400)
    def test_register_existing_email(self):
        response = self.client.post(
        '/api/v2/users/login',
         data = json.dumps(dict(
            self.logged_in_admin)),
         content_type = 'application/json')
        response_data = json.loads(response.data.decode())

        token = json.loads(response.data.decode())['access_token']

        response = self.client.post('/api/v2/auth/user',
        data = json.dumps(self.register_user_existing),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"Email exists")
        self.assertEqual(response.status_code, 200)

    def register_user_empty_email(self):
        token = json.loads(response.data.decode())['access_token']
        response = self.client.post(
        '/api/v2/users',
        data = json.dumps({}),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json'
        )
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"Fields cannot be empty")
        self.assertEqual(response.status_code, 200)

    def test_Register_Valid_email(self):

        response = self.client.post(
        '/api/v2/users/login',
         data = json.dumps(dict(
            email='<EMAIL>',password='<PASSWORD>')),
         content_type = 'application/json')
        response_data = json.loads(response.data.decode())

        token = json.loads(response.data.decode())['access_token']

        response = self.client.post(
        '/api/v2/auth/user',
        data = json.dumps(self.register_user),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json')
        response_data = json.loads(response.data)
        # self.assertEqual(response_data["message"],"Email exists")
        self.assertEqual(response.status_code, 201)
    def test_Register_user(self):
        response = self.client.post(
        '/api/v2/users/login',
         data = json.dumps(dict(
            self.logged_in_admin)),
         content_type = 'application/json')
        response_data = json.loads(response.data.decode())

        token = json.loads(response.data.decode())['access_token']

        response = self.client.post('/api/v2/auth/user',
        data = json.dumps(self.register_invalid_email),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"Enter correct email format")
        self.assertEqual(response.status_code, 200)

    def test_Register_user_no_email(self):
        response = self.client.post(
        '/api/v2/users/login',
         data = json.dumps(dict(
            email='<EMAIL>',password='<PASSWORD>')),
         content_type = 'application/json')
        response_data = json.loads(response.data.decode())

        token = json.loads(response.data.decode())['access_token']

        response = self.client.post('/api/v2/auth/user',
        data = json.dumps(self.register_user_without_email),
        headers=dict(Authorization="Bearer " + token),
        content_type = 'application/json')
        response_data = json.loads(response.data)
        self.assertEqual(response_data["message"],"'email' is a required property")
        self.assertEqual(response.status_code, 400)

   
drop_tables()