Exemplo n.º 1
0
    def delete(self, project_id: int):
        """
        Unsets a project as favorite
        ---
        tags:
            - favorites
        produces:
            - application/json
        parameters:
            - in: header
              name: Authorization
              description: Base64 encoded session token
              required: true
              type: string
              default: Token sessionTokenHere==
            - name: project_id
              in: path
              description: Unique project ID
              required: true
              type: integer
        responses:
            200:
                description: New favorite created
            400:
                description: Invalid Request
            401:
                description: Unauthorized - Invalid credentials
            500:
                description: Internal Server Error
        """
        try:
            ProjectService.unfavorite(project_id, token_auth.current_user())
        except NotFound:
            return {"Error": "Project Not Found"}, 404
        except ValueError as e:
            return {"Error": str(e)}, 400
        except Exception as e:
            error_msg = f"Favorite PUT - unhandled error: {str(e)}"
            current_app.logger.critical(error_msg)
            return {"Error": error_msg}, 500

        return {"project_id": project_id}, 200
Exemplo n.º 2
0
    def test_project_favorite(self):
        if self.skip_tests:
            return
        # Validate that project has not been favorited by user.
        fav = ProjectService.is_favorited(self.test_project.id,
                                          self.test_user.id)
        self.assertFalse(fav)

        # Get project favorites from user.
        favorites = UserService.get_projects_favorited(self.test_user.id)
        json_data = favorites.to_primitive()
        self.assertEqual(len(json_data["favoritedProjects"]), 0)

        # Now favorite a new project.
        ProjectService.favorite(self.test_project.id, self.test_user.id)
        fav = ProjectService.is_favorited(self.test_project.id,
                                          self.test_user.id)
        self.assertTrue(fav)

        # Get project favorites from user.
        favorites = UserService.get_projects_favorited(self.test_user.id)
        json_data = favorites.to_primitive()
        self.assertEqual(len(json_data["favoritedProjects"]), 1)

        # Now unfavorite it.
        ProjectService.unfavorite(self.test_project.id, self.test_user.id)
        fav = ProjectService.is_favorited(self.test_project.id,
                                          self.test_user.id)
        self.assertFalse(fav)

        # Unfavorite a project not been favorited previously.
        with self.assertRaises(ValueError):
            ProjectService.unfavorite(self.test_project.id, self.test_user.id)

        # Verify that NotFound exists.
        with self.assertRaises(NotFound):
            ProjectService.is_favorited(100, self.test_user.id)
            ProjectService.favorite(100, self.test_user.id)
            ProjectService.unfavorite(100, self.test_user.id)