Exemple #1
0
 def post(self):
     payload = request.get_json()
     try:
         user_validator.validate_create_payload(payload)
         data = user_controller.create_user(payload)
     except ValidationError as e:
         raise ViewsException(status_code=400, payload=e.messages)
     except UserAlreadyExistException as e:
         raise ViewsException(status_code=422, payload=e.messages)
     
     return jsonify(data)
Exemple #2
0
    def test_validate_create_payload_returns_error_when_email_is_empty(self):
        # Given
        self.payload['email'] = ''

        # When
        with self.assertRaises(ValidationError) as error:
            user_validator.validate_create_payload(self.payload)

        # Then
        self.assertEqual(TestUtils.NOT_VALID_EMAIL_FIELD,
                         error.exception.messages['email'])
Exemple #3
0
    def test_validate_create_payload_raises_error_when_email_is_missing(self):
        # Given
        self.payload.pop('email')

        # When
        with self.assertRaises(ValidationError) as error:
            user_validator.validate_create_payload(self.payload)

        # Then
        self.assertEqual(TestUtils.MISSING_FIELD,
                         error.exception.messages['email'])
Exemple #4
0
    def test_validate_create_payload_raises_error_when_username_is_empty(self):
        # Given
        self.payload['username'] = ''

        # When
        with self.assertRaises(ValidationError) as error:
            user_validator.validate_create_payload(self.payload)

        # Then
        self.assertEqual(TestUtils.SHORTER_FIELD,
                         error.exception.messages['username'])
Exemple #5
0
    def test_validate_create_payload_raises_error_when_email_is_not_defined(
            self):
        # Given
        self.payload['email'] = None

        # When
        with self.assertRaises(ValidationError) as error:
            user_validator.validate_create_payload(self.payload)

        # Then
        self.assertEqual(TestUtils.NULL_FIELD,
                         error.exception.messages['email'])
Exemple #6
0
    def test_validate_create_payload_raises_error_when_username_is_bad_format(
            self):
        # Given
        self.payload['username'] = {}

        # When
        with self.assertRaises(ValidationError) as error:
            user_validator.validate_create_payload(self.payload)

        # Then
        self.assertEqual(TestUtils.NOT_VALID_STRING_FIELD,
                         error.exception.messages['username'])