def test_public_create_region(self):
        '''
        Public - Without authorization headers - Cannot create regions
        '''
        test_payload = generate_region_payload()
        res = self.client().post('/regions', json=test_payload)
        self.assertEqual(res.status_code, 401)

        '''
        Services Manager - Cannot create regions
        '''
        test_payload = generate_region_payload()
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().post('/regions', json=test_payload,
                                 headers=self.headers)
        self.assertEqual(res.status_code, 401)

        '''
        CHIEF OFFICER - Can create regions
        '''
        test_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post('/regions', json=test_payload,
                                 headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
    def test_delete_a_region(self):
        # Create Region by Chief Officer
        test_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post('/regions', json=test_payload,
                                 headers=self.headers)
        self.assertEqual(res.status_code, 200)
        data = json.loads(res.data)
        created_region_id = data['created']

        # Public cannot delete a region
        res = self.client().delete('/regions/' + str(created_region_id))
        self.assertEqual(res.status_code, 401)

        # Services Manager cannot delete a region
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().delete(
            '/regions/' + str(created_region_id),
            headers=self.headers)
        self.assertEqual(res.status_code, 401)

        # Chief Officer can delete a region
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().delete(
            '/regions/' + str(created_region_id),
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['deleted'])
    def test_delete_a_service(self):
        # Create a new region by chief officer
        test_region_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post(
            '/regions',
            json=test_region_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_region_id = data['created']

        # Create a new service by Services Manager
        test_payload = generate_service_payload(created_region_id)
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().post('/services', json=test_payload,
                                 headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_service_id = data['created']

        # DELETE A SERVICE
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().delete(
            '/services/' + str(created_service_id),
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['deleted'])

        # Create a new service again by Services Manager
        test_payload = generate_service_payload(created_region_id)
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().post('/services', json=test_payload,
                                 headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_service_id = data['created']

        # DELETE A PARENT REGION - Cascade delete region and its services -
        # Only by Chief Officer
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().delete(
            '/regions/' + str(created_region_id),
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['deleted'])
    def test_update_a_service(self):
        # Create a new region by chief officer
        test_region_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post(
            '/regions',
            json=test_region_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_region_id = data['created']

        # Create a new service by Services Manager
        test_payload = generate_service_payload(created_region_id)
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().post('/services', json=test_payload,
                                 headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_service_id = data['created']

        # UPDATE A SERVICE BY CHIEF OFFICER
        test_update_service_payload = {
            "address": "Test Address",
            "phone": "+91-1112223344"
        }
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().patch(
            '/services/' + str(created_service_id),
            json=test_update_service_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['updated'])

        # UPDATE A SERVICE BY SERVICES MANAGER
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().patch('/services/' + str(created_service_id),
                                  json=test_update_service_payload,
                                  headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['updated'])

        # UPDATE A SERVICE BY PUBLIC - Not allowed
        res = self.client().patch(
            '/services/' + str(created_service_id),
            json=test_update_service_payload)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)
    def test_update_a_region(self):
        # Create Region by Chief Officer
        test_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post('/regions', json=test_payload,
                                 headers=self.headers)
        self.assertEqual(res.status_code, 200)
        data = json.loads(res.data)
        created_region_id = data['created']

        test_payload = {
            "regionhead": "AnithaKarunakaran"
        }

        # UPDATE A REGION BY PUBLIC - NOT ALLOWED
        res = self.client().patch('/regions/' + str(created_region_id),
                                  json=test_payload)
        self.assertEqual(res.status_code, 401)

        # UPDATE A REGION BY SERVICES MANAGER - NOT ALLOWED
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().patch(
            '/regions/' +
            str(created_region_id),
            json=test_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)

        # UPDATE A REGION BY CHIEF OFFICER
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().patch(
            '/regions/' +
            str(created_region_id),
            json=test_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['updated'])
    def test_read_a_service(self):

        # Create a new region by chief officer
        test_region_payload = generate_region_payload()
        self.headers.update({'Authorization': 'Bearer ' + CHIEF_OFFICER_JWT})
        res = self.client().post(
            '/regions',
            json=test_region_payload,
            headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_region_id = data['created']

        # Create a new service by Services Manager
        test_payload = generate_service_payload(created_region_id)
        self.headers.update(
            {'Authorization': 'Bearer ' + SERVICES_MANAGER_JWT})
        res = self.client().post('/services', json=test_payload,
                                 headers=self.headers)
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['created'])
        created_service_id = data['created']

        # Read a service
        res = self.client().get('/services/' + str(created_service_id))
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['service'])

        # Read all services
        res = self.client().get('/services')
        self.assertEqual(res.status_code, 200)