Exemplo n.º 1
0
    def test_post_create_post(self, test_client):
        """
        Test POST request to the /community/_/post/create route to assert the post is
        created successfully.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/create",
            data={
                "title": "mockposttitle",
                "post": "mockpost"
            },
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created post" in response.data
Exemplo n.º 2
0
    def test_get_update_post(self, test_client):
        """
        Test GET request to the /community/_/post/_/update route to assert the post
        update page is displayed.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get(
            f"/community/{community.name}/post/{post.title}/update")

        assert response is not None
        assert response.status_code == 200
        assert b"Update Post" in response.data
Exemplo n.º 3
0
    def test_post_downvote_post(self, test_client):
        """
        Test POST request to the /community/_/post/_/downvote route to assert the user
        successfully downvotes the post.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/downvote")

        assert response is not None
        assert response.status_code == 302
        post_vote = PostVote.query.filter_by(user_id=app_user.id,
                                             post_id=post.id).first()
        assert post_vote is not None
        assert post_vote.vote == -1
Exemplo n.º 4
0
    def test_post_reply(self, test_client):
        """
        Test POST request to the /community/_/post/_/reply route to assert the reply is
        successfully created.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/reply",
            data={"reply": "mockreply"},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created reply" in response.data
Exemplo n.º 5
0
    def test_get_feed(self, test_client):
        """
        Test GET request to the / route to assert posts from the users joined
        communities are displayed.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        community_member = CommunityMember(app_user=app_user,
                                           community=community)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(community_member)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get("/")

        assert response is not None
        assert response.status_code == 200
        assert bytes(post.title, "utf-8") in response.data
Exemplo n.º 6
0
    def setUp(self):
        app.config.from_object(app_config['testing'])
        # seting up testing configurations
        self.test_app = app.test_client()
        self.business = {
            "business_name": "lisaP",
            "business_location": "ngara",
            "business_category": "service"
        }  #contains bussiness parameters to be passed
        #signs up a test user
        signup_response = signup(self)
        #asserts that he has been signed in
        self.assertEqual(signup_response.status_code, 201)
        # login our test user
        login_response = login(self)
        # asserts that he is logged in
        self.assertEqual(login_response.status_code, 200)
        # get the response data,and deserialize it(which is msg and token)
        response_data = json.loads(login_response.data)
        # assert that that token exsists in response data
        self.assertIn("access_token", response_data)
        # store the token for later use
        self.authorization = dict(Authorization="Bearer " +
                                  response_data["access_token"])
        # we register a business using the authorization token
        self.register_business = self.test_app.post(  #post is a request(containing endpoint,required authorization)
            "/api/v1/business",
            # pass the authorization to header(define nature of request) and bearer
            headers=self.authorization,
            data=json.dumps(self.business),
            content_type="application/json")

        user_info.clear()
Exemplo n.º 7
0
    def test_get_create_community(self, test_client):
        """
        Test GET request to the /community/create route to assert the community
        creation page is returned.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get("/community/create")

        assert response is not None
        assert response.status_code == 200
        assert b"Create Community" in response.data
Exemplo n.º 8
0
    def test_post_logout(self, test_client):
        """
        Test POST request to the /logout route to assert the user is successfully
        logged out.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post("/logout", follow_redirects=True)

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully logged out" in response.data
Exemplo n.º 9
0
    def test_get_update_community(self, test_client):
        """
        Test GET request to the /community/_/update route to assert the community
        update page is returned.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get(f"/community/{community.name}/update")

        assert response is not None
        assert response.status_code == 200
        assert b"Update Community" in response.data
Exemplo n.º 10
0
    def test_post_create_community(self, test_client):
        """
        Test POST request to the /community/create route to assert the community is
        successfully created.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            "/community/create",
            data={"name": "mockcommunity", "description": "mockdescription"},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created community" in response.data
Exemplo n.º 11
0
    def test_post_leave_community(self, test_client):
        """
        Test POST request to the /community/_/leave route to assert the user
        successfully left the community.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(f"/community/{community.name}/leave",
                                    follow_redirects=True)

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully left community" in response.data
Exemplo n.º 12
0
    def test_post_upvote_reply(self, test_client):
        """
        Test POST request to the /community/_/post/_/reply/_/upvote route to assert the
        user successfuly upvotes the reply.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        reply = Reply(reply="mockreply", app_user=app_user, post=post)
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.add(reply)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/reply/{reply.id}/upvote"
        )

        assert response is not None
        assert response.status_code == 302
        reply_vote = ReplyVote.query.filter_by(
            user_id=app_user.id, reply_id=reply.id
        ).first()
        assert reply_vote is not None
        assert reply_vote.vote == 1