示例#1
0
    def test_patch_about_details_succeeds(self):
        """
        Test that the endpoint '/about/create_or_update' updates about details when a patch request is
        made.
        :return: None
        """
        new_role = RoleFactory.create(name='admin')

        UserRoleFactory.create(user_id=self.user_id(), role_id=new_role.id)

        AboutFactory.create(
            details=base64.b64encode(self.html_data['data'].encode('utf-8')))

        response = self.client().patch(
            self.make_url("/about/create_or_update"),
            headers=self.headers(),
            data=self.encode_to_json_string(self.html_data_update))

        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['data']["details"],
                         self.html_data_update["data"])
示例#2
0
    def test_update_faq_succeeds(self):
        new_role = RoleFactory.create(name='admin')

        new_user_role = UserRoleFactory.create(user_id=self.user_id(),
                                               role=new_role)

        faq = FaqFactory()
        update_faq_info = FaqFactory.build()

        faq_data = dict(question=update_faq_info.question,
                        answer=update_faq_info.answer)

        response = self.client().patch(
            self.make_url(f"/faqs/{faq.id}"),
            headers=self.headers(),
            data=self.encode_to_json_string(faq_data))

        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['faq']['question'],
                         update_faq_info.question)
        self.assertEqual(response_json['payload']['faq']['answer'],
                         update_faq_info.answer)

        self.assertEqual(response_json['payload']['faq']['question'],
                         faq.question)
        self.assertEqual(response_json['payload']['faq']['answer'], faq.answer)
示例#3
0
    def test_new_user_role_method_returns_new_user_role_object(self):
        role = RoleFactory.create()
        location = LocationFactory.create()
        user_role = UserRoleFactory.build(role_id=role.id, location=location)

        new_user_role = self.repo.new_user_role(user_role.role_id,
                                                user_role.user_id,
                                                user_role.location_id,
                                                user_role.email)

        self.assertIsInstance(new_user_role, UserRole)
        self.assertEqual(str(new_user_role.user_id), str(user_role.user_id))
        self.assertEqual(new_user_role.location.id, user_role.location.id)
示例#4
0
    def test_exclude_works_user_role_instance(self):
        role = RoleFactory.create()
        location = LocationFactory.create()
        user_role = UserRoleFactory.build(role_id=role.id,
                                          location_id=location.id)

        new_user_role = self.repo.new_user_role(user_role.role_id,
                                                user_role.user_id,
                                                user_role.location_id,
                                                user_role.email)

        excluded_response = new_user_role.to_dict(exclude=["user_id"])

        self.assertFalse(excluded_response.get("user_id", False))
示例#5
0
    def test_exclude_works_user_role_instance(self):
        role = RoleFactory.create()
        user = UserFactory.create()

        user_role = UserRoleFactory.build(
            role_id=role.id,
            user_id=user.id,
        )

        new_user_role = self.repo.new_user_role(
            user_id=user_role.user_id,
            role_id=user_role.role_id,
        )

        excluded_response = new_user_role.to_dict(exclude=["user_id"])

        self.assertFalse(excluded_response.get("user_id", False))
示例#6
0
    def test_delete_faq_succeeds(self):

        new_role = RoleFactory.create(name='admin')

        new_user_role = UserRoleFactory.create(user_id=self.user_id(),
                                               role=new_role)

        faq = FaqFactory()

        response = self.client().delete(self.make_url(f"/faqs/{faq.id}"),
                                        headers=self.headers())

        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_json['msg'], 'FAQ deleted successfully')
示例#7
0
    def test_run_5_minute_method(self):
        role = RoleFactory.create()
        location = LocationFactory.create()
        user_role = UserRoleFactory.build(role_id=role.id, location=location)

        UserRoleRepo().new_user_role(user_role.role_id, user_role.user_id,
                                     user_role.location_id, user_role.email)

        Cron(self.app).run_5_minute()

        results = self.redis_set.get(user_role.email[0])
        self.assertEqual(user_role.email, results[0])

        results = self.redis_set.get(user_role.email[0:1])
        self.assertEqual(user_role.email, results[0])

        results = self.redis_set.get(user_role.email[0:2])
        self.assertEqual(user_role.email, results[0])
示例#8
0
    def test_create_about_endpoint_rejects_invalid_data(self):
        """
        Test that the endpoint '/about/create_or_update' rejects json request without data field
        :return: None
        """
        new_role = RoleFactory.create(name='admin')

        UserRoleFactory.create(user_id=self.user_id(), role=new_role)

        response = self.client().post(
            self.make_url("/about/create_or_update"), headers=self.headers(),
            data=self.encode_to_json_string(self.html_data_wrong_key)
        )

        response_json = self.decode_from_json_string(response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response_json['msg'], 'Bad Request - data is required')
示例#9
0
    def test_new_user_role_updates_cache(self):
        role = RoleFactory.create()
        location = LocationFactory.create()
        user_role = UserRoleFactory.build(role_id=role.id, location=location)

        self.repo.new_user_role(user_role.role_id, user_role.user_id,
                                user_role.location_id, user_role.email)

        results = self.redis_set.get(user_role.email[0:1])
        self.assertTrue(user_role.email in results)

        results = self.redis_set.get(user_role.email[0:3])
        self.assertTrue(user_role.email in results)

        results = self.redis_set.get(user_role.email[0:5])
        self.assertTrue(user_role.email in results)

        results = self.redis_set.get(user_role.email[0:len(user_role.email) -
                                                     1])
        self.assertTrue(user_role.email in results)
示例#10
0
    def test_create_about_details_succeeds(self):
        """
        Test that the endpoint '/about/create_or_update' creates about details when a post request is
        made in case details do not already exist in the database
        :return: None
        """
        new_role = RoleFactory.create(name='admin')

        UserRoleFactory.create(user_id=self.user_id(), role=new_role)

        response = self.client().post(
            self.make_url("/about/create_or_update"), headers=self.headers(),
            data=self.encode_to_json_string(self.html_data)
        )

        response_json = self.decode_from_json_string(response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['data']["details"], self.html_data["data"])
示例#11
0
 def create_admin(self):
     role = RoleFactory.create(name='admin')
     user_id = self.user_id()
     UserRoleFactory.create(user_id=user_id, role=role)
示例#12
0
def create_user_role(keyword, role_name):
    role = RoleFactory.create(name=role_name)
    user_id = BaseTestCase.user_id()
    PermissionFactory.create(keyword=keyword, role=role)
    return UserRoleFactory.create(user_id=user_id, role=role), user_id