def test_schema(self):
        """ Check Project Schema output """
        content = "lorem"
        p = Project(content=content)
        p_dump = ProjectSchema().dump(p)

        self.assertEquals(p_dump["content"], content)
    def test_feed_get_projects(self):
        """ Test getting projects from feed. """
        from app.models.content import Project, Category

        # Implement unit test

        # Create 2 categories
        categories = [
            Category(name="math"),
            Category(name="physics"),
        ]

        db.session.bulk_save_objects(categories)
        db.session.flush()

        # Create projects
        projects = [
            Project(content=fake.text(), category_id=1),
            Project(content=fake.text(), category_id=2),
            Project(content=fake.text(), category_id=2),
        ]

        db.session.bulk_save_objects(projects)
        db.session.commit()

        math_feed_resp = get_feed_projects(self, 1, "math")
        math_feed_resp_data = json.loads(math_feed_resp.data.decode())

        self.assertTrue(math_feed_resp.status)
        self.assertEqual(math_feed_resp.status_code, 200)
        self.assertEqual(len(math_feed_resp_data["projects"]), 1)

        physx_feed_resp = get_feed_projects(self, 1, "physics")
        physx_feed_resp_data = json.loads(physx_feed_resp.data.decode())

        self.assertTrue(physx_feed_resp.status)
        self.assertEqual(physx_feed_resp.status_code, 200)
        self.assertEqual(len(physx_feed_resp_data["projects"]), 2)
Example #3
0
    def test_post_create(self):
        """ Test post creation """
        # Create a mock user
        from app.models.user import User

        author = User(username="******")

        db.session.add(author)
        db.session.commit()

        # Create a mock project
        from app.models.content import Project

        project_public_id = 4206921
        project = Project(content="hemlo", public_id=project_public_id)

        db.session.add(project)
        db.session.commit()

        access_token = create_access_token(author.id)

        # Create post
        from hashlib import sha256

        image_hash = sha256(str("an_image").encode("utf-8")).hexdigest()[:32]
        data = dict(caption="A caption.", image_hash=image_hash)

        create_resp = create_post(self, access_token, project_public_id, data)
        create_resp_data = json.loads(create_resp.data.decode())

        post = create_resp_data["post"]

        self.assertTrue(create_resp.status)
        self.assertEqual(create_resp.status_code, 201)

        # Check if each field matches.
        for field in data:
            self.assertEqual(post[field], data[field])

        # Test for bad request
        data.pop("caption")
        bad_create_resp = create_post(self, access_token, project_public_id,
                                      data)

        self.assertEqual(bad_create_resp.status_code, 400)
Example #4
0
    def test_project_get(self):
        """ Test getting a project from DB """
        data = dict(content="Brogramming", public_id=123)
        project = Project(content=data["content"], public_id=data["public_id"])

        db.session.add(project)
        db.session.commit()

        project_resp = get_project_data(self, data["public_id"])
        project_data = json.loads(project_resp.data.decode())

        self.assertTrue(project_resp.status)
        self.assertEqual(project_resp.status_code, 200)
        self.assertEqual(project_data["project"]["content"], data["content"])

        # Test a 404 request
        project_404_resp = get_project_data(self, 69)
        self.assertEqual(project_404_resp.status_code, 404)
Example #5
0
    def create(data, current_user):
        # Assign the vars
        title = data["title"]
        difficulty = data["difficulty"]
        time_required = data["time_required"]

        abstract = data["abstract"]
        objective = data["objective"]
        safety = data.get("safety")
        content = data["content"]

        category_id = data["category_id"]

        # Check if current_user is a creator.
        if not current_user.has_role(Permission.CREATE):
            return err_resp("User is not a creator.", "user_not_creator", 403)

        # Create a new project
        try:
            from uuid import uuid4
            from .utils import create_and_load

            project = Project(
                public_id=str(uuid4().int)[:15],
                creator_id=current_user.id,
                title=title,
                difficulty=difficulty,
                time_required=time_required,
                abstract=abstract,
                objective=objective,
                safety=safety,
                content=content,
                category_id=category_id,
            )

            project_data = create_and_load(project)

            resp = message(True, "Project created.")
            resp["project"] = project_data
            return resp, 201

        except Exception as error:
            current_app.logger.error(error)
            return internal_err_resp
Example #6
0
    def test_project_delete(self):
        """ Test Project deletion """
        # Create a mock user & project
        from app.models.user import User

        u = User(email="*****@*****.**", password="******")

        db.session.add(u)
        db.session.commit()

        p = Project(creator_id=u.id, public_id=123)

        db.session.add(p)
        db.session.commit()

        access_token = create_access_token(identity=u.id)

        delete_resp = delete_project(self, access_token, p.public_id)
        self.assertTrue(delete_resp.status)
        self.assertEqual(delete_resp.status_code, 200)

        # Check if project is gone ;(
        query_project = Project.query.filter_by(public_id=p.public_id).first()
        self.assertIsNone(query_project)
    def test_public_ids_are_random(self):
        """ Check if project public IDs are random """
        p = Project(public_id=str(uuid4().int)[:15])
        p2 = Project(public_id=str(uuid4().int)[:15])

        self.assertNotEquals(p, p2)