Exemplo n.º 1
0
def test_delete_image_of_different_product(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    product_without_image = Product(name='Different product', price=2.99)
    categories.add_product(category, product_without_image)

    image = products.add_image(product, url='fake_url.jpg')

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    assert product.id

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')
    payload = r.json
    access_token = payload['access_token']

    r = client.delete(
        f'/api/products/{product_without_image.id}/images/{image.id}',
        headers={'Authorization': f'Bearer {access_token}'})
    payload = r.json

    assert r.status_code == status.HTTP_404_NOT_FOUND
    assert payload['message'] == 'Image not found.'
Exemplo n.º 2
0
def test_get_all_products(client):
    category = categories.add(Category(name='Men'))
    categories.add_product(category, Product(name='Product One', price=13.99))
    product = Product(name='Product Two', price=23.99, description='blah')
    categories.add_product(category, product)
    categories.add_product(category, Product(name='Product Three', price=3.99))
    categories.add_product(category, Product(name='Product Four', price=68.99))
    products.delete(product)

    r = client.get('/api/products/')

    payload = r.json

    all_products = payload

    assert r.status_code == status.HTTP_200_OK
    assert len(all_products) == 3

    sorted_products = sorted(all_products, key=lambda product_: product_['id'])

    assert sorted_products[0]['name'] == 'Product One'
    assert sorted_products[1]['name'] == 'Product Three'
    assert sorted_products[2]['name'] == 'Product Four'

    assert sorted_products[0]['price'] == 13.99
    assert sorted_products[1]['price'] == 3.99
    assert sorted_products[2]['price'] == 68.99

    assert sorted_products[0]['description'] is None
    assert sorted_products[1]['description'] is None
    assert sorted_products[2]['description'] is None
Exemplo n.º 3
0
def test_delete_category_with_products(client):
    category = categories.add(Category(name='Men'))
    categories.add_product(category, Product(name='Product 1', price=1.99))
    categories.add_product(category, Product(name='Product 2', price=2.99))
    categories.add_product(category, Product(name='Product 3', price=3.99))

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    assert category.id

    r = client.post(
        '/api/auth/login',
        data=json.dumps({
            'email': '*****@*****.**',
            'password': '******'
        }),
        content_type='application/json'
    )
    payload = r.json
    access_token = payload['access_token']

    r = client.delete(f'/api/categories/{category.id}',
                      headers={'Authorization': f'Bearer {access_token}'})
    payload = r.json

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert payload['message'] == 'Category contains products.'
Exemplo n.º 4
0
def seed_db():
    from project.business import users
    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    from project.business import categories
    category = categories.add(Category('Men'))

    categories.add_product(category, Product(name='Super product',
                                             price=19.99))
    categories.add_product(category,
                           Product(name='Very bad product', price=2.99))
Exemplo n.º 5
0
def test_delete_not_existing_product_rating(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    assert len(product.ratings) == 0

    r = client.delete(f'/api/products/{product.id}/ratings',
                      headers={'Authorization': f'Bearer {access_token}'})

    payload = r.json

    assert r.status_code == status.HTTP_404_NOT_FOUND
    assert payload['message'] == 'Rating not found.'
Exemplo n.º 6
0
def test_add_product_rating_second_time(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    client.post(f'/api/products/{product.id}/ratings',
                data=json.dumps({'rating': 5}),
                headers={'Authorization': f'Bearer {access_token}'},
                content_type='application/json')

    assert product.ratings[0].user == user

    r = client.post(f'/api/products/{product.id}/ratings',
                    data=json.dumps({'rating': 5}),
                    headers={'Authorization': f'Bearer {access_token}'},
                    content_type='application/json')

    payload = r.json

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert payload['message'] == 'This user already rated this product.'
Exemplo n.º 7
0
def test_delete_product(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    assert product.id

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')
    payload = r.json
    access_token = payload['access_token']

    assert not product.is_deleted

    r = client.delete(f'/api/products/{product.id}',
                      headers={'Authorization': f'Bearer {access_token}'})
    payload = r.json

    assert r.status_code == status.HTTP_200_OK
    assert payload['message'] == 'Product was successfully deleted.'
    assert product.is_deleted
Exemplo n.º 8
0
def test_add_product_image_not_admin_or_worker(client):
    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True

    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    assert product.id

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    assert user.role != UserRole.WORKER and user.role != UserRole.ADMIN

    with open(testing_image_jpg_path, 'rb') as f:
        r = client.post(f'/api/products/{product.id}/images',
                        data={'file': (f, f.name)},
                        headers={'Authorization': f'Bearer {access_token}'},
                        content_type='multipart/form-data')

    payload = r.json

    assert r.status_code == status.HTTP_403_FORBIDDEN
    assert payload[
        'message'] == 'You do not have permission to perform this action.'
Exemplo n.º 9
0
def test_delete_image_of_product_no_admin_or_worker(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    image = products.add_image(product, url='fake_url.jpg')

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True

    assert product.id
    assert user.role != UserRole.ADMIN and user.role != UserRole.WORKER

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')
    payload = r.json
    access_token = payload['access_token']

    r = client.delete(f'/api/products/{product.id}/images/{image.id}',
                      headers={'Authorization': f'Bearer {access_token}'})
    payload = r.json

    assert r.status_code == status.HTTP_403_FORBIDDEN
    assert payload[
        'message'] == 'You do not have permission to perform this action.'
Exemplo n.º 10
0
def test_update_product_missing_category_id(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    assert product.name == 'Super Small Product'

    r = client.put(f'/api/products/{product.id}',
                   data=json.dumps({
                       'name': 'Super Big Product',
                       'price': 0.99
                   }),
                   headers={'Authorization': f'Bearer {access_token}'},
                   content_type='application/json')

    payload = r.json

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert payload['message'] == 'Invalid payload.'
Exemplo n.º 11
0
def test_add_product_rating(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    r = client.post(f'/api/products/{product.id}/ratings',
                    data=json.dumps({'rating': 5}),
                    headers={'Authorization': f'Bearer {access_token}'},
                    content_type='application/json')

    payload = r.json

    assert r.status_code == status.HTTP_201_CREATED
    assert payload['message'] == 'Rating was successfully added.'
    assert len(product.ratings) == 1
    assert product.ratings[0] is not None
    assert product.ratings[0].rating == 5
Exemplo n.º 12
0
def test_add_product_image_not_allowed_file_ext(client):
    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    assert product.id

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    with open(testing_image_png_path, 'rb') as f:
        r = client.post(f'/api/products/{product.id}/images',
                        data={'file': (f, f.name)},
                        headers={'Authorization': f'Bearer {access_token}'},
                        content_type='multipart/form-data')

    payload = r.json

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert payload['message'] == 'File extension not allowed.'
Exemplo n.º 13
0
def test_add_product_image(client):
    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    assert product.id

    r = client.post('/api/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json')

    payload = r.json

    access_token = payload['access_token']

    with open(testing_image_jpg_path, 'rb') as f:
        r = client.post(f'/api/products/{product.id}/images',
                        data={'file': (f, f.name)},
                        headers={'Authorization': f'Bearer {access_token}'},
                        content_type='multipart/form-data')

    payload = r.json

    assert r.status_code == status.HTTP_201_CREATED
    assert payload['message'] == 'Image was successfully uploaded.'
    assert len(product.images) == 1
    assert isinstance(product.images[0], ProductImage)
    assert product.images[0].url
Exemplo n.º 14
0
def test_get_single_product(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Product', price=99.99)
    categories.add_product(category, product)

    r = client.get(f'/api/products/{product.id}')

    payload = r.json

    assert r.status_code == status.HTTP_200_OK
    assert payload['name'] == 'Super Product'
    assert payload['price'] == 99.99
    assert payload['description'] is None
Exemplo n.º 15
0
def test_add_product_rating_not_logged_in(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    r = client.post(f'/api/products/{product.id}/ratings',
                    data=json.dumps({'rating': 5}),
                    content_type='application/json')

    payload = r.json

    assert r.status_code == status.HTTP_403_FORBIDDEN
    assert payload[
        'message'] == 'You do not have permission to perform this action.'
Exemplo n.º 16
0
def test_get_single_already_deleted_product(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Product', price=99.99)
    categories.add_product(category, product)
    products.delete(product)

    assert product.is_deleted

    r = client.get(f'/api/products/{product.id}')

    payload = r.json

    assert r.status_code == status.HTTP_404_NOT_FOUND
    assert payload['message'] == 'Product not found.'
Exemplo n.º 17
0
def test_delete_product_not_logged_in(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    assert product.id

    r = client.delete(f'/api/products/{product.id}')
    payload = r.json

    assert r.status_code == status.HTTP_403_FORBIDDEN
    assert payload[
        'message'] == 'You do not have permission to perform this action.'
Exemplo n.º 18
0
def test_get_product_ratings(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    user1 = users.add(User(email='*****@*****.**', password='******'))
    user2 = users.add(User(email='*****@*****.**', password='******'))
    user3 = users.add(User(email='*****@*****.**', password='******'))

    assert user1.id == 1
    assert user2.id == 2
    assert user3.id == 3

    products.add_rating(product, user1, 5)
    products.add_rating(product, user2, 4)
    products.add_rating(product, user3, 3)

    r = client.get(f'/api/products/{product.id}/ratings')

    payload = r.json

    product_ratings = payload

    assert r.status_code == status.HTTP_200_OK
    assert len(product_ratings) == 3

    sorted_product_ratings = sorted(product_ratings,
                                    key=lambda rating: rating['user']['id'])

    assert sorted_product_ratings[0]['user']['id'] == 1
    assert sorted_product_ratings[1]['user']['id'] == 2
    assert sorted_product_ratings[2]['user']['id'] == 3

    assert sorted_product_ratings[0]['user']['email'] == '*****@*****.**'
    assert sorted_product_ratings[1]['user']['email'] == '*****@*****.**'
    assert sorted_product_ratings[2]['user']['email'] == '*****@*****.**'

    assert sorted_product_ratings[0]['product']['id'] == product.id
    assert sorted_product_ratings[1]['product']['id'] == product.id
    assert sorted_product_ratings[2]['product']['id'] == product.id

    assert sorted_product_ratings[0]['rating'] == 5
    assert sorted_product_ratings[1]['rating'] == 4
    assert sorted_product_ratings[2]['rating'] == 3
Exemplo n.º 19
0
def test_get_images_of_product(client):
    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    products.add_image(product, url='fake_url.jpg')
    products.add_image(product, url='fake_url2.jpg')
    products.add_image(product, url='fake_url3.jpg')

    r = client.get(f'/api/products/{product.id}/images')
    payload = r.json

    assert r.status_code == status.HTTP_200_OK

    images = payload

    assert len(images) == 3

    for image in images:
        assert 'fake_url' in image['url']
Exemplo n.º 20
0
def test_add_product_image_not_logged_in(client):
    user = users.add(User(email='*****@*****.**', password='******'))
    user.active = True
    user.role = UserRole.ADMIN

    category = categories.add(Category(name='Men'))
    product = Product(name='Super Small Product', price=0.99)
    categories.add_product(category, product)

    assert product.id

    with open(testing_image_jpg_path, 'rb') as f:
        r = client.post(f'/api/products/{product.id}/images',
                        data={'file': (f, f.name)},
                        content_type='multipart/form-data')

    payload = r.json

    assert r.status_code == status.HTTP_403_FORBIDDEN
    assert payload[
        'message'] == 'You do not have permission to perform this action.'
Exemplo n.º 21
0
    def post(self, category_id):
        data = request.get_json()

        if not data:
            raise InvalidPayload

        name = data.get('name')
        price = data.get('price')
        description = data.get('description')

        if name is None or price is None:
            raise InvalidPayload

        category = categories.get(category_id)

        if category is None:
            raise NotFound('Category not found.')

        product = Product(name=name, price=price, description=description)
        categories.add_product(category, product)

        return {
            'message': 'Product was successfully added.'
        }, status.HTTP_201_CREATED
Exemplo n.º 22
0
def seed_products(num_of_products, num_of_cats):
    category_list = seed_and_get_category(num_of_cats)
    subcategories = seed_and_get_category(3)

    category_obj = category_list[0]
    for sc in subcategories:
        category_obj.sub_categories.append(sc)

    category_obj.save()

    category_list = category_list + subcategories
    for i in range(num_of_products):
        product = Product()
        product.title = random_string()
        product.description = random_string()
        product.price = random.randint(1000, 100000)
        product.is_available = random_bool()
        product.is_discount = random_bool()
        product.quantity = random.randint(0, 100)
        product.category = random.choice(category_list)
        product.weight = random.uniform(0, 100)
        product.height = random.uniform(0, 100)
        product.width = random.uniform(0, 100)

        product.save()
Exemplo n.º 23
0
def delete(product: Product):
    product.is_deleted = True
    session.commit()