def post(self, story_id): try: # get token from header token = self._get_token_from_header() # identify with token username = Token.identify(token) # get data from request comment_text = self._get_comment_text_from_request() timestamp = self._get_timestamp_from_request() # sanitize data timestamp = InputSanitizer.sanitize_positive_integer(timestamp) # post reaction to story by story_id saved_comment = Story.comment_on_story(story_id, username, comment_text, timestamp) # generate response output = saved_comment # return response return ResponseBuilder.build_response(output) except (InvalidTokenException, ExpiredTokenException, InvalidFormatException, MissingFieldException, StoryNotFoundException) as e: return ResponseBuilder.build_error_response(e.message, e.error_code)
def test_failed_sanitize_positive_integer_including_0(self): data_input = "-2" with self.assertRaises(InvalidFormatException) as context: InputSanitizer.sanitize_positive_integer(data_input, True) exception = context.exception self.assertEqual(exception.error_code, 400)
def test_successful_sanitize_positive_integer_including_0(self): data_input = "0" expected_output = 0 self.assertEqual( InputSanitizer.sanitize_positive_integer(data_input, True), expected_output)
def test_successful_sanitize_positive_integer_excluding_0(self): data_input = "13212" expected_output = 13212 self.assertEqual(InputSanitizer.sanitize_positive_integer(data_input), expected_output)