Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
(PC-1647) Add tests on route DELETE stocks/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
abel-andre committed Mar 21, 2019
1 parent 4dc5b92 commit fccb0c7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 33 deletions.
8 changes: 4 additions & 4 deletions routes/stocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
load_or_404, \
login_or_api_key_required
from domain.keywords import LANGUAGE
from validation.stocks import check_request_has_offer_id, check_new_stock_has_dates, check_existing_stock_has_dates
from validation.stocks import check_request_has_offer_id, check_dates_are_allowed_on_new_stock, check_dates_are_allowed_on_existing_stock

search_models = [
# Order is important
Expand Down Expand Up @@ -66,7 +66,7 @@ def create_stock():
check_request_has_offer_id(request_data)
offer_id = dehumanize(request_data.get('offerId', None))
offer = find_offer_by_id(offer_id)
check_new_stock_has_dates(request_data, offer)
check_dates_are_allowed_on_new_stock(request_data, offer)
offerer = offerer_queries.get_by_offer_id(offer_id)
ensure_current_user_has_rights(RightsType.editor, offerer.id)

Expand All @@ -84,7 +84,7 @@ def edit_stock(stock_id):
request_data = request.json
query = Stock.queryNotSoftDeleted().filter_by(id=dehumanize(stock_id))
stock = query.first_or_404()
check_existing_stock_has_dates(request_data, stock.offer)
check_dates_are_allowed_on_existing_stock(request_data, stock.offer)
offerer_id = stock.resolvedOffer.venue.managingOffererId
ensure_current_user_has_rights(RightsType.editor, offerer_id)
stock.populateFromDict(request_data)
Expand All @@ -109,6 +109,6 @@ def delete_stock(id):
except MailServiceException as e:
app.logger.error('Mail service failure', e)

PcObject.check_and_save(*(bookings + [stock]))
PcObject.check_and_save(stock, *bookings)

return jsonify(stock._asdict()), 200
87 changes: 74 additions & 13 deletions tests/routes/stocks_id_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from models import Booking
from models.pc_object import PcObject, serialize
from tests.conftest import clean_database, TestClient
from tests.test_utils import API_URL, create_booking, create_user, create_user_offerer, create_offerer, create_venue, \
Expand All @@ -23,14 +24,13 @@ def when_user_is_admin(self, app):
humanized_stock_id = humanize(stock.id)

# when
request = TestClient().with_auth('test@email.com')\
request = TestClient().with_auth('test@email.com') \
.get(API_URL + '/stocks/' + humanized_stock_id)
# then
assert request.status_code == 200
assert request.json()['available'] == 10
assert request.json()['price'] == 10


class Returns404:
@clean_database
def when_stock_is_soft_deleted(self, app):
Expand All @@ -43,7 +43,7 @@ def when_stock_is_soft_deleted(self, app):
humanized_stock_id = humanize(stock.id)

# when
request = TestClient().with_auth('test@email.com')\
request = TestClient().with_auth('test@email.com') \
.get(API_URL + '/stocks/' + humanized_stock_id)

# then
Expand All @@ -65,12 +65,13 @@ def when_user_has_editor_rights_on_offerer(self, app):
humanized_stock_id = humanize(stock.id)

# when
request_update = TestClient().with_auth('test@email.com')\
request_update = TestClient().with_auth('test@email.com') \
.patch(API_URL + '/stocks/' + humanized_stock_id, json={'available': 5, 'price': 20})

# then
assert request_update.status_code == 200
request_after_update = TestClient().with_auth('test@email.com').get(API_URL + '/stocks/' + humanized_stock_id)
request_after_update = TestClient().with_auth('test@email.com').get(
API_URL + '/stocks/' + humanized_stock_id)
assert request_after_update.json()['available'] == 5
assert request_after_update.json()['price'] == 20

Expand All @@ -85,16 +86,16 @@ def when_user_is_admin(self, app):
humanized_stock_id = humanize(stock.id)

# when
request_update = TestClient().with_auth('test@email.com')\
request_update = TestClient().with_auth('test@email.com') \
.patch(API_URL + '/stocks/' + humanized_stock_id, json={'available': 5, 'price': 20})

# then
assert request_update.status_code == 200
request_after_update = TestClient().with_auth('test@email.com').get(API_URL + '/stocks/' + humanized_stock_id)
request_after_update = TestClient().with_auth('test@email.com').get(
API_URL + '/stocks/' + humanized_stock_id)
assert request_after_update.json()['available'] == 5
assert request_after_update.json()['price'] == 20


class Returns400:
@clean_database
def when_wrong_type_for_available(self, app):
Expand All @@ -109,7 +110,7 @@ def when_wrong_type_for_available(self, app):
PcObject.check_and_save(booking, user_admin)

# when
response = TestClient().with_auth('email@test.com')\
response = TestClient().with_auth('email@test.com') \
.patch(API_URL + '/stocks/' + humanize(stock.id), json={'available': ' '})

# then
Expand All @@ -128,7 +129,7 @@ def when_booking_limit_datetime_after_beginning_datetime(self, app):
serialized_date = serialize(stock.beginningDatetime + timedelta(days=1))

# when
response = TestClient().with_auth('email@test.com')\
response = TestClient().with_auth('email@test.com') \
.patch(API_URL + '/stocks/' + humanize(stockId), json={'bookingLimitDatetime': serialized_date})

# then
Expand Down Expand Up @@ -167,14 +168,13 @@ def when_available_below_number_of_already_existing_bookings(self, app):
PcObject.check_and_save(booking, user_admin)

# when
response = TestClient().with_auth('email@test.com')\
response = TestClient().with_auth('email@test.com') \
.patch(API_URL + '/stocks/' + humanize(stock.id), json={'available': 0})

# then
assert response.status_code == 400
assert 'available' in response.json()


class Returns403:
@clean_database
def when_user_has_no_rights(self, app):
Expand All @@ -186,9 +186,70 @@ def when_user_has_no_rights(self, app):
PcObject.check_and_save(user, stock)

# when
response = TestClient().with_auth('test@email.com')\
response = TestClient().with_auth('test@email.com') \
.patch(API_URL + '/stocks/' + humanize(stock.id), json={'available': 5})

# then
assert response.status_code == 403
assert 'Cette structure n\'est pas enregistrée chez cet utilisateur.' in response.json()['global']


@pytest.mark.standalone
class Delete:
class Returns200:
@clean_database
def when_current_user_has_rights_on_offer(self, app):
# given
user = create_user(email='test@email.com')
offerer = create_offerer()
user_offerer = create_user_offerer(user, offerer)
venue = create_venue(offerer)
stock = create_stock_with_event_offer(offerer, venue)
PcObject.check_and_save(user, stock, user_offerer)

# when
response = TestClient().with_auth('test@email.com') \
.delete(API_URL + '/stocks/' + humanize(stock.id))

# then
assert response.status_code == 200
assert response.json()['isSoftDeleted'] is True

@clean_database
def when_bookings_exist_on_stock(self, app):
# given
user = create_user(email='test@email.com')
other_user = create_user(email='consumer@test.com')
offerer = create_offerer()
user_offerer = create_user_offerer(user, offerer)
venue = create_venue(offerer)
stock = create_stock_with_event_offer(offerer, venue, price=0)
booking1 = create_booking(other_user, stock=stock, is_cancelled=False)
booking2 = create_booking(other_user, stock=stock, is_cancelled=False)
PcObject.check_and_save(user, stock, user_offerer, booking1, booking2)

# when
TestClient().with_auth('test@email.com') \
.delete(API_URL + '/stocks/' + humanize(stock.id))

# then
bookings = Booking.query.filter_by(isCancelled=True).all()
assert booking1 in bookings
assert booking2 in bookings

class Returns403:
@clean_database
def when_current_user_has_no_rights_on_offer(self, app):
# given
user = create_user(email='test@email.com')
offerer = create_offerer()
venue = create_venue(offerer)
stock = create_stock_with_event_offer(offerer, venue)
PcObject.check_and_save(user, stock)

# when
response = TestClient().with_auth('test@email.com') \
.delete(API_URL + '/stocks/' + humanize(stock.id))

# then
assert response.status_code == 403
28 changes: 14 additions & 14 deletions tests/validation_stocks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from models.pc_object import serialize
from tests.test_utils import create_thing_offer, create_event_offer
from utils.human_ids import humanize
from validation.stocks import check_new_stock_has_dates, check_existing_stock_has_dates
from validation.stocks import check_dates_are_allowed_on_new_stock, check_dates_are_allowed_on_existing_stock


@pytest.mark.standalone
class CheckNewStockHasDatesTest:
class CheckDatesAreAllowedOnNewStockTest:
class OfferIsOnThingTest:
def test_raises_error_with_beginning_and_end_datetimes(self):
# Given
Expand All @@ -26,7 +26,7 @@ def test_raises_error_with_beginning_and_end_datetimes(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['global'] == [
Expand All @@ -46,7 +46,7 @@ def test_raises_error_with_beginning_datetime_only(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['global'] == [
Expand All @@ -66,7 +66,7 @@ def test_raises_error_with_end_datetime_only(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['global'] == [
Expand All @@ -87,7 +87,7 @@ def test_raises_error_with_missing_end_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['endDatetime'] == [
Expand All @@ -108,7 +108,7 @@ def test_raises_error_with_none_end_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['endDatetime'] == [
Expand All @@ -128,7 +128,7 @@ def test_raises_error_with_missing_beginning_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['beginningDatetime'] == [
Expand All @@ -147,15 +147,15 @@ def test_raises_error_with_none_beginning_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_new_stock_has_dates(data, offer)
check_dates_are_allowed_on_new_stock(data, offer)

# Then
assert e.value.errors['beginningDatetime'] == [
'Ce paramètre est obligatoire'
]


class CheckExistingStockHasDatesTest:
class CheckDatesAreAllowedOnExistingStockTest:
class OfferIsOnThingTest:
def test_raises_error_with_beginning_datetime_only(self):
# Given
Expand All @@ -164,7 +164,7 @@ def test_raises_error_with_beginning_datetime_only(self):

# When
with pytest.raises(ApiErrors) as e:
check_existing_stock_has_dates(data, offer)
check_dates_are_allowed_on_existing_stock(data, offer)

# Then
assert e.value.errors['global'] == [
Expand All @@ -178,7 +178,7 @@ def test_raises_error_with_end_datetime_only(self):

# When
with pytest.raises(ApiErrors) as e:
check_existing_stock_has_dates(data, offer)
check_dates_are_allowed_on_existing_stock(data, offer)

# Then
assert e.value.errors['global'] == [
Expand All @@ -193,7 +193,7 @@ def test_raises_error_with_none_beginning_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_existing_stock_has_dates(data, offer)
check_dates_are_allowed_on_existing_stock(data, offer)

# Then
assert e.value.errors['beginningDatetime'] == [
Expand All @@ -207,7 +207,7 @@ def test_raises_error_with_none_end_datetime(self):

# When
with pytest.raises(ApiErrors) as e:
check_existing_stock_has_dates(data, offer)
check_dates_are_allowed_on_existing_stock(data, offer)

# Then
assert e.value.errors['endDatetime'] == [
Expand Down
4 changes: 2 additions & 2 deletions validation/stocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def check_request_has_offer_id(request_data: dict):
raise ApiErrors({'offerId': ['Ce paramètre est obligatoire']})


def check_new_stock_has_dates(request_data: dict, offer: Offer):
def check_dates_are_allowed_on_new_stock(request_data: dict, offer: Offer):
if offer.thing:
_forbid_dates_on_stock_for_thing_offer(request_data)
else:
Expand All @@ -31,7 +31,7 @@ def check_new_stock_has_dates(request_data: dict, offer: Offer):
raise ApiErrors({'beginningDatetime': ['Ce paramètre est obligatoire']})


def check_existing_stock_has_dates(request_data: dict, offer: Offer):
def check_dates_are_allowed_on_existing_stock(request_data: dict, offer: Offer):
if offer.thing:
_forbid_dates_on_stock_for_thing_offer(request_data)
else:
Expand Down

0 comments on commit fccb0c7

Please sign in to comment.