import datetime
import glob
import os

from django.utils import timezone

from authenticate.models import User
from collaborators import defined_roles as dr, conf
from collaborators.models import Access, InviteAccess
from institute.models import Institute
from plan.models import UserPlan, Environment, InstitutePlan
from wikiazma.myTest import MyTestCase


class CollaboratorsTest(MyTestCase):

    def setUp(self):
        self.setUpHelpers()
        # self.setBaseUser()
        (self.owner_user, self.owner_token) = self.authenticateHelper.new_user_with_email(email="owner_user@nisodynamic.com")
        (self.admin_user, self.super_admin_token) = self.authenticateHelper.new_user_with_email(email="super_admin_user@nisodynamic.com")
        (self.content_writer_user, self.content_writer_token) = self.authenticateHelper.new_user_with_email(
            email="content_writer_user@nisodynamic.com")
        (self.content_reader_user, self.content_reader_token) = self.authenticateHelper.new_user_with_email(
            email="content_reader_user@nisodynamic.com")
        (self.no_role_user, self.no_role_token) = self.authenticateHelper.new_user_with_email(email="no_role_user@nisodynamic.com")
        (self.invite_user, self.invite_token) = self.authenticateHelper.new_user_with_email(email="invite_user@nisodynamic.com")
        (self.another_admin_user, self.another_admin_token) = self.authenticateHelper.new_user_with_email(
            email="another_admin_user@nisodynamic.com")
        (self.another_content_writer_user, self.another_content_writer_token) = self.authenticateHelper.new_user_with_email(
            email="another_content_writer_user@nisodynamic.com")
        (self.another_content_reader_user, self.another_content_reader_token) = self.authenticateHelper.new_user_with_email(
            email="another_content_reader_user@nisodynamic.com")

        UserPlan.objects.create(user_id=self.owner_user.id, name='test_plan', start_date=timezone.now(), max_institutes_per_user=1000)
        self.institute = Institute.objects.create(user=self.owner_user, name='some')
        InstitutePlan.objects.create(name='test_plan', start_date=timezone.now(), institute=self.institute,
                                     max_collaborators_per_institute=1000)

        self.owner_user_access = Access.objects.create(user=self.owner_user, institute=self.institute, roles=[dr.__ROLE_OWNER__])
        self.admin_user_access = Access.objects.create(user=self.admin_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        self.content_writer_user_access = Access.objects.create(user=self.content_writer_user, institute=self.institute,
                                                                roles=[dr.__ROLE_ADMIN__])
        self.content_reader_user_access = Access.objects.create(user=self.content_reader_user, institute=self.institute,
                                                                roles=[dr.__ROLE_ADMIN__])
        self.another_admin_user_access = Access.objects.create(user=self.another_admin_user, institute=self.institute,
                                                               roles=[dr.__ROLE_SUPER_ADMIN__])
        self.another_content_writer_user_access = Access.objects.create(
            user=self.another_content_writer_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        self.another_content_reader_user_access = Access.objects.create(
            user=self.another_content_reader_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])

    def tearDown(self):
        files = glob.glob('test_files/*')
        for file in files:
            if not file.endswith(('/image.png', '\image.png', 'fakepng.png', '.pdf')):
                os.remove(file)
        User.objects.all().delete()

    ############################################################################
    #institute id invalid
    ######### for owner_user with __ROLE_OWNER__ in the institute #########
    def test_invite_owner_user__user_has_not_access(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {
                                                                               'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75',
                                                                               'email': 'ahmad0@yahoo.com',
                                                                               'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Forbidden',error_message)
        
    # Successful
    def test_invite_owner_user__invited_user_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',{
                                                                               'institute_id': self.institute.id ,
                                                                               'email': 'ahmad0@yahoo.com',
                                                                               'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful',error_message)
        (self.invalid_user, self.invalid_token) = self.authenticateHelper.new_user_with_email(email="no_access@nisodynamic.com")
        self.institute = Institute.objects.create(user=self.invalid_user, name='some')
        self.another_admin_user_access = Access.objects.create(user=self.invalid_user, institute=self.institute,
                                                               roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',{
                                                                               'institute_id': self.institute.id ,
                                                                               'email': 'ahmad01@yahoo.com',
                                                                               'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.invalid_token)
        self.assertEqual(response_data['message'], 'Forbidden')
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',{
                                                                               'institute_id': self.institute.id ,
                                                                               'email': 'ahmad0@yahoo.com',
                                                                               'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Forbidden',error_message)
    def test_invite_owner_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]' },
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')
        
        # Successful  Phone
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'phone': '09021729894',
                                                                            'roles':  f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')
        
    # Email or phone
    def test_invite_owner_user_phone__error_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'phone': '09021729894',
                                                                            'email': 'salehdadgar220@gmail.com',
                                                                            'roles':  f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Some fields are not valid')
        # Email upper error
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.owner_user.email.upper(),
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'This user is already a collaborator')
        # Email or phone
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Some fields are not valid')

    def test_invite_owner_user__access_exists_error(self):
        # Has been a member before
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.owner_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'This user is already a collaborator')

    def test_invite_owner_user__access_exists_successful_1(self):
        #Successful
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles':  f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_owner_user__access_exists(self):
        #Has been a member before
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles':  f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'This user is already a collaborator')

    def test_invite_owner_user__invite_access_exists(self):
        #Successful
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles':f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_owner_user__token_error_response(self):
        # invalid token
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.owner_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_invite_owner_user__serializer_error(self):
        # validation_error No fields are filled
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.owner_token)
        self.assertEqual(response_data['status'], 'validation_error')
        #This field is required.
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=self.owner_token)
        self.assertEqual(response_data['fields'] 
                         ['institute_id'], 'This field is required.')
        #This field may not be blank
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles':f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': ''},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field may not be blank.')
        # id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles':f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': '123'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['fields']
                         ['institute_id'], 'Enter a valid id')
        #institute_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]',
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Forbidden')
        #valid email address
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmadyahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['fields']['email'], 'Enter a valid email address')
        #valid roles list 
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmad@yahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': '["__ROLE_SUPER__"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['fields']
                         ['roles'], 'Enter a valid roles list')

    ####################################################################
    ######## for admin_user with __ROLE_ADMIN__ in the institute ########
     #institute_id invalid
    def test_invite_admin_user__user_has_not_access(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {
                                                                               'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75',
                                                                               'email': 'ahmad0@yahoo.com',
                                                                               'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    #user has not permission
    def test_invite_admin_user__user_has_not_permission(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    #Successful
    def test_invite_admin_user__invited_user_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful',error_message)
    #Successful
    def test_invite_admin_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__error_response(self):
        #This user is already invited 
        self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                      'email': self.no_role_user.email,
                                                      'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                             test_token=self.super_admin_token)

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)

        self.assertEqual(response_data['message'], 'This user is already invited')

    def test_invite__attempt_response(self):
        #The number of attempts you make is too much
        for i in range(0,40):
                    (self.test_user, self.test_user_token) = self.authenticateHelper.new_user_with_email(email=f"test_user{i}@nisodynamic.com")
                    InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user, institute=self.institute)


        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': 'saleh@yahoo.com',
                                                                                                    'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
#TODO
        self.assertEqual(response_data['message'], 'The number of attempts you make is too much')
    # 10 rejected Forbidden
    def test_invite__attempt_rejected_response(self):
        for i in range(0,11):
            (self.test_user, self.test_user_token) = self.authenticateHelper.new_user_with_email(email=f"test_user{i}@nisodynamic.com")
            InviteAccess.objects.create(modifire=self.owner_user, rejected=True, invited_user=self.test_user, institute=self.institute)

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': 'saleh@yahoo.com',
                                                                                                    'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)

        self.assertEqual(response_data['message'], 'Forbidden')
    # 10 canceled Forbidden
    def test_invite__attempt_canceled_response(self):
        
        for i in range(0,11):
            (self.test_user, self.test_user_token) = self.authenticateHelper.new_user_with_email(email=f"test_user{i}@nisodynamic.com")
            InviteAccess.objects.create(modifire=self.owner_user, canceled=True, invited_user=self.test_user, institute=self.institute)

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': 'saleh@yahoo.com',
                                                                                                    'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)

        self.assertEqual(response_data['message'], 'Forbidden')
# This user is already a collaborator
    def test_invite_admin_user__access_exists_error(self):
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'This user is already a collaborator')

    def test_invite_admin_user__access_exists_successful_1(self):
        #Successful admin token
        # self.no_role_user_access = Access.objects.create(
        #     user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_CONTENT_WRITER__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__access_exists_successful_2(self):
        # self.no_role_user_access = Access.objects.create(
        #     user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_CONTENT_READER__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__invite_access_exists(self):
        #Successful 
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__invite_access_exists_successful_1(self):
        #Successful 
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__invite_access_exists_successful_2(self):
        #Successful 
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_invite_admin_user__token_error_response(self):
        # token invalid
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.super_admin_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_invite_admin_user__serializer_error(self):
        # validation_error email is None and roles 
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.super_admin_token)
        self.assertEqual(response_data['status'], 'validation_error')
         # This field is required. institute_id 
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']
                         ['institute_id'], 'This field is required.')
        # institute_id in None
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': ''},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(
            response_data['fields']['institute_id'], 'This field may not be blank.')
        # institute_id Enter a valid id       
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': '123'},
                                                                  test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']
                         ['institute_id'], 'Enter a valid id')
        # institute_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]',
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')
        # email invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmadyahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']
                         ['email'], 'Enter a valid email address')
        # roles invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmad@yahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': '["__INVALID_ROLE__"]'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']
                         ['roles'], 'Enter a valid roles list')

    ############################################################################
    ######## for content_writer_user with __ROLE_CONTENT_WRITER__ in the institute ########
    # institute_id invalid
    def test_invite_content_writer_user__user_has_not_access(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75',
            'email': 'ahmad0@yahoo.com',
            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    # invite content writer user user has not permission ADMIN
    def test_invite_content_writer_user__user_has_not_permission_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    # invite content writer user user has not permission WRITER and READER
    def test_invite_content_writer_user__user_has_not_permission_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    # invite content writer user user has not permission READER
    def test_invite_content_writer_user__invited_user_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    # invite content writer user user has not permission READER &&Email is different from the above
    def test_invite_content_writer_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__access_exists_error_1(self):
        # invite content writer user user has not permission READER &&Email is different from the above
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__access_exists_error_2(self):
        #cheak permission
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__access_exists_error_3(self):
        #cheak permission
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__access_exists_error_4(self):
        #cheak permission
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__invite_access_exists_error_1(self):
        #cheak permission
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__invite_access_exists_error_2(self):
        #cheak permission
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__invite_access_exists_error_3(self):
        #cheak permission
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__invite_access_exists_error_4(self):
        #cheak permission
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_invite_content_writer_user__token_error_response(self):
        # token invalid
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.content_writer_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_invite_content_writer_user__serializer_error(self):
        #validation_error roles and email or phone is None
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.content_writer_token)
        self.assertEqual(response_data['status'], 'validation_error')
        #This field is required.
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field is required.')
        #institute_id is None
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': ''},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field may not be blank.')

         #institute_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': '123'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['institute_id'], 'Enter a valid id')

         #institute_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]',
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

         #email invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmadyahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['email'], 'Enter a valid email address')
        #roles invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmad@yahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': '["__INVALID_ROLE__"]'},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['roles'], 'Enter a valid roles list')

    #############################################################################
    ######## for content_reader_user with __ROLE_CONTENT_READER__ in the institute ########
    #invite content reader user user has not access && institute_id invalid
    def test_invite_content_admin_user__user_has_not_access(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75',
            'email': 'ahmad0@yahoo.com',
            'roles': f'["{dr.__ROLE_ADMIN__}"]'}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    #cheak permission
    def test_invite_content_admin_user__user_has_not_permission_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__user_has_not_permission_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__user_has_not_permission_3(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__invited_user_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': 'ahmad0@yahoo.com',
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__access_exists_error_1(self):
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__access_exists_error_2(self):
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__access_exists_successful(self):
        self.no_role_user_access = Access.objects.create(
            user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__invite_access_exists_error_1(self):
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__invite_access_exists_error_2(self):
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
#cheak permission
    def test_invite_content_admin_user__invite_access_exists_successful(self):
        self.no_role_user_invite_access = InviteAccess.objects.create(
            modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute, roles=[dr.__ROLE_ADMIN__])
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')
    #token invalid
    def test_invite_content_admin_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.content_reader_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)
    #validation_error This field is required
    def test_invite_content_reader_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invite', test_token=self.content_reader_token)
        self.assertEqual(response_data['status'], 'validation_error')
        
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': ''},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field may not be blank.')
        #institute_id in valid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]', 'institute_id': '123'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['institute_id'], 'Enter a valid id')
        #institute_id in valid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {
            'email': 'ahmad@yahoo.com', 'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]',
            'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

        #email in valid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmadyahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['email'], 'Enter a valid email address')
        #roles in valid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'email': 'ahmad@yahoo.com',
                                                                            'institute_id': self.institute.id,
                                                                            'roles': '["__INVALID_ROLE__"]'},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['roles'], 'Enter a valid roles list')

    ############################################################################
    #The number of attempts you make is too much
    def test_number_of_non_accepted_invitations_exceeds_the_limit(self):
        (self.test_user1, self.test_user1_token) = self.authenticateHelper.new_user_with_email(email="test_user1@nisodynamic.com")
        (self.test_user2, self.test_user2_token) = self.authenticateHelper.new_user_with_email(email="test_user2@nisodynamic.com")
        (self.test_user3, self.test_user3_token) = self.authenticateHelper.new_user_with_email(email="test_user3@nisodynamic.com")
        (self.test_user4, self.test_user4_token) = self.authenticateHelper.new_user_with_email(email="test_user4@nisodynamic.com")
        (self.test_user5, self.test_user5_token) = self.authenticateHelper.new_user_with_email(email="test_user5@nisodynamic.com")
        (self.test_user6, self.test_user6_token) = self.authenticateHelper.new_user_with_email(email="test_user6@nisodynamic.com")
        (self.test_user7, self.test_user7_token) = self.authenticateHelper.new_user_with_email(email="test_user7@nisodynamic.com")
        (self.test_user8, self.test_user8_token) = self.authenticateHelper.new_user_with_email(email="test_user8@nisodynamic.com")
        (self.test_user9, self.test_user9_token) = self.authenticateHelper.new_user_with_email(email="test_user9@nisodynamic.com")
        (self.test_user10, self.test_user10_token) = self.authenticateHelper.new_user_with_email(email="test_user10@nisodynamic.com")
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user1, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user2, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user3, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user4, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user5, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user6, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user7, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user8, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user9, institute=self.institute)
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.test_user10, institute=self.institute)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.no_role_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual('error', response_data['status'], error_message)
        self.assertEqual('The number of attempts you make is too much',
                         response_data['message'], error_message)
    #invitations list Successful
    def test_invitations_list(self):
        self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                      'email': self.no_role_user.email,
                                                      'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                             test_token=self.owner_token)
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/list', {'skip': 0, 'take': 10, 'institute_id': self.institute.id}, test_token=self.owner_token)

        self.assertEqual(response_data['status'], 'ok', error_message)
        self.assertEqual(response_data['message'], 'Successful', error_message)
        self.assertEqual(1, len(response_data['data']['invitations']), error_message)
    #my_invitations_list Successful
    def test_my_invitations_list(self):
        self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                      'email': self.no_role_user.email,
                                                      'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                             test_token=self.owner_token)
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/my_invitation/list', {'skip': 0, 'take': 10}, test_token=self.no_role_token)

        self.assertEqual(response_data['status'], 'ok', error_message)
        self.assertEqual(response_data['message'], 'Successful', error_message)
        self.assertEqual(1, len(response_data['data']['invitations']), error_message)
        

        
    #test_accept_invite_invite_access_record_not_exists_successful
    def test_accept_invite_invite_access_record_not_exists_successful(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.no_role_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept',
                                                                           {'invitation_id': response_data['access']['id']},
                                                                           test_token=self.no_role_token)
        self.assertEqual(response_data['status'], 'ok', error_message)
        self.assertEqual(response_data['message'], 'Successful', error_message)
    def test_accept_invite_invite_access_record_not_exists_error(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.no_role_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.post_with_token('/collaborator/invitation/accept',
                             {'invitation_id': response_data['access']['id']},
                             test_token=self.no_role_token)
        #We cant find your invitation for this institute!
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept',
                                                                           {'invitation_id': response_data['access']['id']},
                                                                           test_token=self.no_role_token)
        self.assertEqual(response_data['status'], 'error', error_message)
        self.assertEqual(response_data['message'], "We cant find your invitation for this institute!", error_message)

    def test_accept_invite_invite_access_record_error(self):
        #We cant find your invitation for this institute! && invitation_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept',
                                                                           {'invitation_id': '09b301de-7091-4f8d-9458-abccab22e65d'},

                                                                           test_token=self.no_role_token)

        self.assertEqual(response_data['status'], 'error', error_message)
        self.assertEqual(response_data['message'], "We cant find your invitation for this institute!", error_message)

    def test_accept_invite_token_error_response(self):
        
        # token invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept',
                                                                           test_token=self.invite_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_invitation_reject_successful(self):
        # Successful
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.no_role_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/reject',
                                                                           {'invitation_id': response_data['access']['id']},
                                                                           test_token=self.no_role_token)
        self.assertEqual(response_data['status'], 'ok', error_message)
        self.assertEqual(response_data['message'], 'Successful', error_message)

    def test_invitation_reject_error(self):
         # invitation_id invalid
        self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                      'email': self.no_role_user.email,
                                                      'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                             test_token=self.owner_token)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/reject',
                                                                           {'invitation_id': '09b301de-7091-4f8d-9458-abccab22e65d'},
                                                                           test_token=self.no_role_token)
        self.assertEqual(response_data['status'], 'error', error_message)
        self.assertEqual(response_data['message'], 'We cant find your invitation for this institute!', error_message)

    ############################################################################################################
    def test_content_reader_user__access_record_not_exists(self):
        # invitation not found  invitation_id invalid
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.content_reader_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual("invitation not found", response_data['message'], error_message)
    # content reader user
    def test_content_reader_user__error_response_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.owner_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_content_reader_user__error_response_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.admin_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_content_reader_user__error_response_3(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.content_writer_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_content_reader_user__error_response_4(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.content_reader_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_content_reader_user__error_response_5(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.another_content_reader_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_content_reader_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', test_token=self.content_reader_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_content_reader_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove',
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['invitation_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', {'invitation_id': ''}, test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['invitation_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', {'invitation_id': '1234'}, test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['invitation_id'], 'Enter a valid id')

    def test_owner_user__access_record_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.owner_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual('invitation not found', response_data['message'], error_message)

    def test_owner_user__error_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': self.owner_user_access.id}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'invitation not found')

    def test_owner_user__successful_response_2(self):
        # Successful
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        (response, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': response_data['access']['id']}, test_token=self.owner_token)
        self.assertEqual(response['message'], 'Successful')

    def test_owner_user__successful_response_3(self):
        # Successful    
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                           {'institute_id': self.institute.id,
                                                                            'email': self.no_role_user.email,
                                                                            'roles': f'["{dr.__ROLE_ADMIN__}","{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)

        (response, status_code, error_message) = self.post_with_token('/collaborator/invitation/remove', {
            'invitation_id': response_data['access']['id']}, test_token=self.owner_token)
        self.assertEqual(response['message'], 'Successful')

    def test_owner_user__token_error_response(self):
        # Authorization failed token invalid
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', test_token=self.owner_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)
    # This field is required invitation_id
    def test_owner_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', test_token=self.owner_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['invitation_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', {'invitation_id': ''}, test_token=self.owner_token)
        self.assertEqual(response_data['fields']['invitation_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/invitation/remove', {'invitation_id': '1234'}, test_token=self.owner_token)
        self.assertEqual(response_data['fields']['invitation_id'], 'Enter a valid id')

    ######### for owner_user with __ROLE_OWNER__ in the institute #########

    def test_owner_user_access_record_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.owner_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual('Access not found', response_data['message'], error_message)

    def test_owner_user_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.owner_user_access.id}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_owner_user_successful_response_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.admin_user_access.id}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_owner_user_successful_response_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_writer_user_access.id}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_owner_user_successful_response_3(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_reader_user_access.id}, test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_owner_user_token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', test_token=self.owner_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_owner_user_serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', test_token=self.owner_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['access_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': ''}, test_token=self.owner_token)
        self.assertEqual(response_data['fields']['access_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': '1234'}, test_token=self.owner_token)
        self.assertEqual(response_data['fields']['access_id'], 'Enter a valid id')

    ####################################################################
    ####### for admin_user with __ROLE_ADMIN__ in the institute ########

    def test_admin_user__access_record_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.super_admin_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual('Access not found', response_data['message'], error_message)

    def test_admin_user__error_response_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.owner_user_access.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_admin_user__error_response_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.admin_user_access.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_admin_user__error_response_3(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.another_admin_user_access.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_admin_user__successful_response_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_writer_user_access.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_admin_user__successful_response_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_reader_user_access.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_admin_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', test_token=self.super_admin_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_admin_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove',
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['access_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': ''}, test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']['access_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': '1234'}, test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']['access_id'], 'Enter a valid id')

    ############################################################################
    ######## for content_writer_user with __ROLE_CONTENT_WRITER__ in the institute ########

    def test_content_writer_user__access_record_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.content_writer_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual('Access not found', response_data['message'], error_message)

    def test_content_writer_user__error_response(self):
        # content writer cannot remove owner
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.owner_user_access.id}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

        # content writer cannot remove admin
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.admin_user_access.id}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

        # content writer cannot remove himself
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_writer_user_access.id}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

        # content writer cannot remove another content writer
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.another_content_writer_user_access.id}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

        # content writer cannot remove another content reader
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_reader_user_access.id}, test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_writer_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', test_token=self.content_writer_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_content_writer_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove',
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['access_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': ''}, test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['access_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': '1234'}, test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['access_id'], 'Enter a valid id')

    #############################################################################
    ######## for content_reader_user with __ROLE_CONTENT_READER__ in the institute ########

    def test_content_reader_user_access_record_not_exists(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'}, test_token=self.content_reader_token)
        self.assertEqual(404, status_code, error_message)
        self.assertEqual('not_found', response_data['status'], error_message)
        self.assertEqual('Access not found', response_data['message'], error_message)

    def test_content_reader_user_error_response_1(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.owner_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_reader_user_error_response_2(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.admin_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_reader_user_error_response_3(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_writer_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_reader_user_error_response_4(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.content_reader_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_reader_user_error_response_5(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove', {
            'access_id': self.another_content_reader_user_access.id}, test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_content_reader_user_token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', test_token=self.content_reader_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_content_reader_user_serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/remove',
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['access_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': ''}, test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['access_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/remove', {'access_id': '1234'}, test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['access_id'], 'Enter a valid id')

    #
    # ############################################################################
    #
    ######### for owner_user with __ROLE_OWNER__ in the institute #########

    def test_all_collaborators_owner_user__successful_response(self):
        for sort_type in ["inst_name_asc", "inst_name_desc", "inst_created_asc", "inst_created_desc", "email_asc", "email_desc"]:
            (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                               {'institute_id': self.institute.id,
                                                                                'skip': 2,
                                                                                'take': 3, 'sort_by': sort_type},
                                                                               test_token=self.owner_token)
            self.assertEqual(response_data['message'], 'Successful')

    def test_all_collaborators_owner_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.owner_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_all_collaborators_owner_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.owner_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['institute_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': ''}, test_token=self.owner_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': -1},
            test_token=self.owner_token)
        self.assertEqual(response_data['fields']['skip'], 'Ensure this value is greater than or equal to 0.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': 2, 'take': 200},
            test_token=self.owner_token)
        self.assertEqual(response_data['fields']['take'], 'Ensure this value is less than or equal to 100.')

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id,
                                                                            'skip': 2, 'take': 3},
                                                                           test_token=self.owner_token)
        self.assertEqual(response_data['status'], 'ok')

    ####################################################################
    ######## for admin_user with __ROLE_ADMIN__ in the institute ########

    def test_all_collaborators_admin_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id, 'skip': 2,
                                                                            'take': 3, 'sort_by': 'inst_name_asc'},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_all_collaborators_admin_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.super_admin_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_all_collaborators_admin_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.super_admin_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['institute_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': ''}, test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']['institute_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': -1},
            test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']['skip'], 'Ensure this value is greater than or equal to 0.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': 2, 'take': 200},
            test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']['take'], 'Ensure this value is less than or equal to 100.')

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id, 'skip': 2,
                                                                            'take': 3},
                                                                           test_token=self.super_admin_token)
        self.assertEqual(response_data['status'], 'ok')

    ############################################################################
    ######## for content_writer_user with __ROLE_CONTENT_WRITER__ in the institute ########

    def test_all_collaborators_content_writer_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id, 'skip': 2,
                                                                            'take': 3},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_all_collaborators_content_writer_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.content_writer_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_all_collaborators_content_writer_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.content_writer_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['institute_id'],
                         'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': ''}, test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['institute_id'],
                         'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': -1},
            test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['skip'],
                         'Ensure this value is greater than or equal to 0.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': 2, 'take': 200},
            test_token=self.content_writer_token)
        self.assertEqual(response_data['fields']['take'],
                         'Ensure this value is less than or equal to 100.')

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id,
                                                                            'skip': 2, 'take': 3},
                                                                           test_token=self.content_writer_token)
        self.assertEqual(response_data['status'], 'ok')

    #############################################################################
    ######## for content_reader_user with __ROLE_CONTENT_READER__ in the institute ########

    def test_all_collaborators_content_reader_user__successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id, 'skip': 2,
                                                                            'take': 3},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_all_collaborators_content_reader_user__token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.content_reader_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_all_collaborators_content_reader_user__serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', test_token=self.content_reader_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']['institute_id'],
                         'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': ''}, test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['institute_id'],
                         'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': -1},
            test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['skip'],
                         'Ensure this value is greater than or equal to 0.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/all', {'institute_id': self.institute.id, 'skip': 2, 'take': 200},
            test_token=self.content_reader_token)
        self.assertEqual(response_data['fields']['take'],
                         'Ensure this value is less than or equal to 100.')

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/all',
                                                                           {'institute_id': self.institute.id, 'skip': 2,
                                                                            'take': 3},
                                                                           test_token=self.content_reader_token)
        self.assertEqual(response_data['status'], 'ok')

    # ############################################################################

    def test_leave_collaborators_access_record_not_exists_1(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': 'dc5cb7eb-2c27-4021-93b3-ae3835a2df75'},
            test_token=self.owner_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_leave_collaborators_access_record_not_exists_2(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': self.institute.id}, test_token=self.no_role_token)
        self.assertEqual(response_data['message'], 'Forbidden')

    def test_leave_collaborators_owner_cant_leave_institute(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': self.institute.id}, test_token=self.owner_token)
        self.assertEqual(
            response_data['message'],
            'You can not leave your institute, you can delete this institute or transfer ownership to another user.')

    def test_leave_collaborators_successful_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': self.institute.id}, test_token=self.super_admin_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_leave_collaborators_token_error_response(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', test_token=self.super_admin_token + 'some123')
        self.assertIn('Authorization failed', response_data['message'], error_message)

    def test_leave_collaborators_serializer_error(self):
        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', test_token=self.super_admin_token)
        self.assertEqual(response_data['status'], 'validation_error')
        self.assertEqual(response_data['fields']
                         ['institute_id'], 'This field is required.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': ''}, test_token=self.super_admin_token)
        self.assertEqual(
            response_data['fields']['institute_id'], 'This field may not be blank.')

        (response_data, status_code, error_message) = self.post_with_token(
            '/collaborator/leave', {'institute_id': '1234'}, test_token=self.super_admin_token)
        self.assertEqual(response_data['fields']
                         ['institute_id'], 'Enter a valid id')

    def test_cleanup_past_invite_access_function(self):
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.content_writer_user, institute=self.institute,
                                    roles=["__ROLE_SUPER_ADMIN__"])
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.content_reader_user, institute=self.institute,
                                    roles=["__ROLE_SUPER_ADMIN__"])

        InviteAccess.objects.update(created_at=timezone.now() - datetime.timedelta(days=conf.invite_access_cleanup_offset_days + 1))
        InviteAccess.objects.create(modifire=self.owner_user, invited_user=self.no_role_user, institute=self.institute,
                                    roles=["__ROLE_SUPER_ADMIN__"])
        InviteAccess.cleanup_past_invite_access()
        self.assertEqual(InviteAccess.objects.count(), 1)

    #
    def test_send_invite_access_then_change_institute_name_and_then_try_to_accept_the_invitation(self):
        request = {'institute_id': self.institute.id, 'email': self.invite_user.email, 'roles': '["__ROLE_ADMIN__"]'}
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', request, test_token=self.owner_token)
        self.assertEqual('ok', response_data['status'], error_message)
        invite_access_record = InviteAccess.objects.get(id=response_data['access']['id'])
        id = invite_access_record.id
        (response_data, status_code, error_message) = self.post_with_token('/institute/edit',
                                                                           {'name': 'another_name', 'institute_id': self.institute.id},
                                                                           test_token=self.owner_token)
        self.assertEqual('ok', response_data['status'], error_message)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept', {'invitation_id': id},
                                                                           test_token=self.invite_token)
        self.assertEqual('ok', response_data['status'], error_message)

    #
    def test_payment_required_for_invite_access(self):
        (new_owner, new_owner_token) = self.authenticateHelper.new_user_with_email()
        test_institute = Institute.objects.create(user=new_owner, name='test_institute')
        Access.objects.create(user=new_owner, institute=test_institute, roles=[dr.__ROLE_OWNER__])

        new_institute_plan = InstitutePlan.objects.create(institute=test_institute, name='test_plan', start_date=timezone.now(),
                                                          max_collaborators_per_institute=5)
        Environment.objects.update(default_max_collaborators_per_institute=5)

        for i in range(new_institute_plan.max_collaborators_per_institute - 1):
            (new_user, new_user_token) = self.authenticateHelper.new_user_with_email()
            Access.objects.create(user=new_user, institute=test_institute, roles=[dr.__ROLE_SUPER_ADMIN__])
        (test_user, test_user_token) = self.authenticateHelper.new_user_with_email()
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite',
                                                                          {'institute_id': test_institute.id, 'email': test_user.email,
                                                                            'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=new_owner_token)

        self.assertEqual('The capacity to send invitations to each institution is 5 people', response_data['message'])  

    #
    def test_send_invite_access_then_transfer_the_ownership_and_then_finish_invite_access(self):
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.invite_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual('ok', response_data['status'], error_message)
        id = InviteAccess.objects.get(id=response_data['access']['id']).id
        self.institute.user = self.admin_user
        self.institute.save()
        # self.instituteHelper.transfer_ownership_of_institute(self.institute.id, self.admin_user.id, self.admin_user.email, self.admin_token)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/accept', {
            'invitation_id': id}, test_token=self.invite_token)
        self.assertEqual(response_data['message'], 'Successful')

    def test_free_user_invite_collaborators(self):
        Environment.objects.update(default_max_collaborators_per_institute=0)
        InstitutePlan.objects.filter(institute=self.institute).update(max_collaborators_per_institute=0)

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.invite_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        self.assertEqual('error', response_data['status'], error_message)
        self.assertEqual('The capacity to send invitations to each institution is 0 people', response_data['message'], error_message)

    def test_invitation_edit(self):

        (response, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.invite_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)
        #Successful
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/invitation/edit',
                                                               {'invitation_id': response['access']['id'],
                                                                'new_roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=self.owner_token)

        self.assertEqual('ok', response_data['status'], error_message)
        self.assertEqual('Successful',response_data['message'], error_message)
        InviteAccess.objects.get(id=response['access']['id'],roles =["__ROLE_SUPER_ADMIN__"])
    def test_collaborator_edit_error(self):
        #test_collaborator_edit_error
        (response, status_code, error_message) = self.post_with_token('/collaborator/invite', {'institute_id': self.institute.id,
                                                                                                    'email': self.invite_user.email,
                                                                                                    'roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'},
                                                                           test_token=self.owner_token)

        (response_data, status_code, error_message) = self.post_with_token('/collaborator/edit',
                                                               {'access_id':  response['access']['id'],
                                                                'new_roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=self.owner_token)
        access = Access.objects.filter(id=response['access']['id'],roles =["__ROLE_SUPER_ADMIN__"])
        if len (access) == 0:
            self.assertEqual('not_found', response_data['status'], error_message)
            self.assertEqual('Access not found',response_data['message'], error_message)

        else:
            access = Access.objects.get(id='ddddddd')
    def test_collaborator_edit_successful(self):
        #test_collaborator_edit_successful
        # self.instituteHelper.transfer_ownership_of_institute(self.institute.id, self.admin_user.id, self.admin_user.email, self.admin_token)
        (response_data, status_code, error_message) = self.post_with_token('/collaborator/edit',
                                                               {'access_id': self.admin_user_access.id,
                                                                'new_roles': f'["{dr.__ROLE_SUPER_ADMIN__}"]'}, test_token=self.owner_token)

        self.assertEqual('ok', response_data['status'], error_message)
        self.assertEqual('Successful',response_data['message'], error_message)
        Access.objects.get(id=self.admin_user_access.id,roles =["__ROLE_SUPER_ADMIN__"])