def test_get_profile_not_found(self): with patch.object(Persistence, "get_one") as mocked_user_get, \ self.assertRaises(UserNotFoundException) as context: mocked_user_get.side_effect = MagicMock(return_value=None) User.get_profile("pedro", "asd") exc = context.exception self.assertEqual(exc.error_code, 404)
def get(self, username): try: # get token from header token = self._get_token_from_header() # identify with token callee_user = Token.identify(token) # get profile info from user username profile = User.get_profile(username, callee_user) # generate response self.logger.info('User profile found for: {}'.format(username)) output = profile # return response return ResponseBuilder.build_response(output) except UserNotFoundException as e: err_msg = "No user found with that name" self.logger.info(err_msg) return ResponseBuilder.build_error_response(err_msg, e.error_code) except (ExpiredTokenException, MissingFieldException, InvalidTokenException) as e: return ResponseBuilder.build_error_response( e.message, e.error_code)
def test_get_profile_successful(self): with patch.object(Persistence, "get_one") as mocked_user_get, \ patch.object(Story, "get_stories_by_username") as mocked_story_get_stories: aux_timestamp = 123123 aux_story1 = dict(story_mock_private_with_reaction) aux_story1["timestamp"] = aux_timestamp aux_story2 = dict(story_mock_private_with_reaction) aux_story2["timestamp"] = aux_timestamp+1 aux_story3 = dict(story_mock_private_with_reaction) aux_story3["timestamp"] = aux_timestamp+2 expected_profile = profile_mock_without_stories_or_friends expected_profile["stories"] = [aux_story3, aux_story2, aux_story1] expected_profile["number of stories"] = 3 mocked_user_get.side_effect = MagicMock(return_value=user_mock_without_stories_or_friends) mocked_story_get_stories.side_effect = MagicMock(return_value=expected_profile["stories"]) self.assertEqual(User.get_profile("asd", "asd"), expected_profile)