Ejemplo n.º 1
0
    def test_create_user_from_dict(self):
        count_before_insert = User.query.count()

        user_dict = dict(
            login='******',
            first_name='Konrad',
            last_name='Biś',
            password='******',
            is_creator=True,
            is_contractor=True,
            is_admin=False
        )

        user = User.create_from_dict(user_dict)
        db_session.add(user)
        db_session.commit()

        count_after_insert = User.query.count()

        self.assertEqual(count_before_insert + 1, count_after_insert)

        user = User.query.filter_by(login='******').first()

        self.assertIsNotNone(user)
        self.assertEqual(user_dict['login'], user.login)
        self.assertEqual(user_dict['first_name'], user.first_name)
        self.assertEqual(user_dict['last_name'], user.last_name)
        self.assertEqual(user_dict['is_creator'], user.is_creator)
        self.assertEqual(user_dict['is_contractor'], user.is_contractor)
        self.assertEqual(user_dict['is_admin'], user.is_admin)
Ejemplo n.º 2
0
    def test_create_user_duplicate(self):
        user_dict = dict(
            login='******',
            first_name='Konrad',
            last_name='Biś',
            password='******',
            is_creator=True,
            is_contractor=True,
            is_admin=False,
            junk='abcd'
        )

        user = User.create_from_dict(user_dict)
        db_session.add(user)
        db_session.commit()

        count_before_insert = User.query.count()

        response = self.client.post(
            '/user',
            data=json.dumps(user_dict),
            headers={'Content-Type': 'application/json'}
        )

        count_after_insert = User.query.count()

        self.assertStatus(response, 409)
        self.assertEqual(count_before_insert, count_after_insert)
Ejemplo n.º 3
0
    def test_update_user_duplicate_data(self):
        user_dict = dict(
            login='******',
            first_name='Konrad',
            last_name='Biś',
            is_creator=True,
            is_contractor=True,
            is_admin=False,
            password='******',
            junk='abcd'
        )

        user = User.create_from_dict(user_dict)
        db_session.add(user)
        db_session.commit()

        user2 = User.query.first()

        response = self.client.put(
            '/user/' + str(user2.id),
            data=json.dumps(user_dict),
            headers={'Content-Type': 'application/json'}
        )

        self.assertStatus(response, 409)

        user2 = User.query.first()

        self.assertNotEqual(user2.login, user_dict['login'])
Ejemplo n.º 4
0
def create_user():
    data = request.get_json()

    try:
        user = User.create_from_dict(data)
        db_session.add(user)
        db_session.commit()
    except IntegrityError:
        db_session.rollback()
        return jsonify(dict(message='Login already in use')), 409

    return jsonify(user.to_dict()), 201
Ejemplo n.º 5
0
    def test_create_user_from_dict_invalid_data(self):
        count_before_insert = User.query.count()

        user_dict = dict(
            login='******',
            last_name='Biś',
            is_creator=True,
            is_contractor=True,
            is_admin=False
        )

        with self.assertRaises(ValidationError):
            user = User.create_from_dict(user_dict)
            db_session.add(user)
            db_session.commit()

        count_after_insert = User.query.count()

        user = User.query.filter_by(login='******').first()

        self.assertIsNone(user)
        self.assertEqual(count_before_insert, count_after_insert)
Ejemplo n.º 6
0
    def test_create_user_from_dict_invalid_type(self):
        count_before_insert = User.query.count()

        user_list = [
            'konbis',
            'Konrad',
            'Biś',
            True,
            True,
            False,
            'abcd'
        ]

        with self.assertRaises(TypeError):
            user = User.create_from_dict(user_list)
            db_session.add(user)
            db_session.commit()

        count_after_insert = User.query.count()

        user = User.query.filter_by(login='******').first()

        self.assertIsNone(user)
        self.assertEqual(count_before_insert, count_after_insert)