"""Functional test for GET /status endpoint.""" from datetime import datetime import json from oto import response as oto_response from oto import status as oto_status from oto.adaptors.flask import flaskify from availability.constants import models from availability.constants import stores from availability.logic import product_submission from tests import utils PRODUCT_ID = 1 SALES_START_DATE = datetime.utcnow() STORE_INTERNAL_ID = 'internal-id' GO_LIVE_DATE = datetime.utcnow() STATUS = models.RELEASE_STATUS_LIVE COUNTRIES = ['UK', 'USA'] def test_get_product_status(test_database, client, mocker): """Assert can get status of products in stores.""" mocker.patch( 'availability.models.product_in_store.product_model.get_product_by_id', return_value=True) expected_stores_data = [] for store_id in stores.STORE_IDS: utils.create_product_per_store( store_id=store_id, product_id=PRODUCT_ID, product_in_store_status=STATUS, sales_start_date=SALES_START_DATE, store_internal_id=STORE_INTERNAL_ID, countries=COUNTRIES, go_live_date=GO_LIVE_DATE, ) expected_stores_data.append({ 'store_id': store_id, 'status': STATUS, 'go_live_date': GO_LIVE_DATE.strftime(models.ISO8601_DATE_FORMAT), 'link': stores.STOREFRONT_URLS[store_id].format( store_internal_id=STORE_INTERNAL_ID), 'countries': COUNTRIES}) expected_status_data = { 'items': [{'product_id': PRODUCT_ID, 'stores': expected_stores_data}]} mocker.patch('availability.handlers.ownership.check_products_ownership') request_headers = { 'Grass-Account-Type': 'test', 'Grass-Account-Id': '1', 'Correlation-Id': 'some id' } status_response = client.get( '/status?product_ids=1', headers=request_headers) assert json.loads(status_response.data.decode()) == expected_status_data def test_get_product_status_returns_error_if_product_not_owned_by_account( test_database, products_data, mocker, client): """Assert error will be returned if product doesn't belong to account.""" creation_response = product_submission.submit_to_poll(products_data) assert creation_response.status == oto_status.NO_CONTENT ok_response = flaskify( oto_response.Response(status=oto_status.OK)) forbidden_response = flaskify( oto_response.Response(status=oto_status.FORBIDDEN)) mocker.patch( 'availability.validation.ownership.request.head', side_effect=[ok_response, forbidden_response]) request_headers = { 'Grass-Account-Type': 'test', 'Grass-Account-Id': '2', 'Correlation-Id': 'some id' } response = client.get('/status?product_ids=1,2', headers=request_headers) assert response.status_code == oto_status.FORBIDDEN