def test_should_return_200_when_one_comand_in_brand_and_product(self): c1 = CompanyFactory( plCapital=100, plWorkers=0, plRnD=100, plRegistered=100, plNotGlobEnt=100, description="TEST", sources="TEST|BBBB", verified=True, is_friend=True, plCapital_notes="AAA", plWorkers_notes="BBB", plRnD_notes="CCC", plRegistered_notes="DDD", plNotGlobEnt_notes="EEE", ) p = ProductFactory.create(code=5900049011829, company=c1, brand__company=c1) response = self.json_request(self.url + "?device_id=TEST-DEVICE-ID&code=" + str(p.code)) self.assertEqual(200, response.status_code, response.content) self.maxDiff = None self.assertEqual( { 'altText': None, 'card_type': 'type_white', 'code': '5900049011829', 'description': 'TEST', 'donate': { 'show_button': True, 'title': 'Potrzebujemy 1 zł', 'url': 'https://klubjagiellonski.pl/zbiorka/wspieraj-aplikacje-pola/', }, 'friend_text': 'To jest przyjaciel Poli', 'is_friend': True, 'name': 'company_official_125', 'plCapital': 100, 'plCapital_notes': 'AAA', 'plNotGlobEnt': 100, 'plNotGlobEnt_notes': 'EEE', 'plRegistered': 100, 'plRegistered_notes': 'DDD', 'plRnD': 100, 'plRnD_notes': 'CCC', 'plScore': 70, 'plWorkers': 0, 'plWorkers_notes': 'BBB', 'product_id': p.pk, 'report_button_text': 'Zgłoś', 'report_button_type': 'type_white', 'report_text': 'Zgłoś jeśli posiadasz bardziej aktualne dane na temat tego produktu', 'sources': {'TEST': 'BBBB'}, }, json.loads(response.content), )
def test_should_return_200_when_polish_and_known_product(self): c = CompanyFactory( plCapital=100, plWorkers=0, plRnD=100, plRegistered=100, plNotGlobEnt=100, description="TEST", sources="TEST|BBBB", verified=True, is_friend=True, plCapital_notes="AAA", plWorkers_notes="BBB", plRnD_notes="CCC", plRegistered_notes="DDD", plNotGlobEnt_notes="EEE", ) p = ProductFactory.create(code=5900049011829, company=c, brand=None) response = self.json_request(self.url + "?device_id=TEST-DEVICE-ID&code=" + str(p.code)) self.assertEqual(200, response.status_code, response.content) self.maxDiff = None self.assertEqual( { "product_id": p.id, "code": "5900049011829", "name": c.official_name, "card_type": "type_white", "plScore": 70, "altText": None, "plCapital": 100, "plCapital_notes": "AAA", "plWorkers": 0, "plWorkers_notes": "BBB", "plRnD": 100, "plRnD_notes": "CCC", "plRegistered": 100, "plRegistered_notes": "DDD", "plNotGlobEnt": 100, "plNotGlobEnt_notes": "EEE", "report_text": "Zg\u0142o\u015b je\u015bli posiadasz bardziej aktualne dane na temat tego produktu", "report_button_text": "Zg\u0142o\u015b", "report_button_type": "type_white", "is_friend": True, "friend_text": "To jest przyjaciel Poli", "description": "TEST", "sources": {"TEST": "BBBB"}, "donate": { "show_button": True, "url": "https://klubjagiellonski.pl/zbiorka/wspieraj-aplikacje-pola/", "title": "Potrzebujemy 1 zł", }, }, json.loads(response.content), )
def test_should_create_simple_report(self): p = ProductFactory.create(ai_pics_count=0) response = self.json_request( self.url + "?device_id=TEST-DEVICE-ID", data={ 'product_id': p.pk, 'files_count': 1, 'file_ext': "png", "mime_type": 'image/jpeg', 'original_width': 1000, 'original_height': 2000, 'width': 100, 'height': 200, 'device_name': 'TEST-device', 'flash_used': True, 'was_portrait': False, }, ) self.assertEqual(200, response.status_code) response_data = response.json() self.assertEqual(len(response_data['signed_requests']), 1) signed_url: str = response_data['signed_requests'][0] self.assertTrue(signed_url.startswith("http://minio:9000")) # Valid signed URL response = requests.put( signed_url, data=_create_image(), headers={"x-amz-acl": "public-read", 'Content-Type': 'image/jpeg'} ) self.assertEqual(200, response.status_code, response.text) # Assert AIPics self.assertEqual(AIPics.objects.count(), 1) ai_pics: AIPics = AIPics.objects.first() self.assertEqual(p.pk, ai_pics.product_id) self.assertEqual("TEST-DEVICE-ID", ai_pics.client) self.assertEqual(1000, ai_pics.original_width) self.assertEqual(2000, ai_pics.original_height) self.assertEqual(100, ai_pics.width) self.assertEqual(200, ai_pics.height) self.assertEqual(True, ai_pics.flash_used) self.assertEqual(False, ai_pics.was_portrait) # Assert AIAttachment self.assertEqual(1, AIAttachment.objects.count()) ai_attachment: AIAttachment = AIAttachment.objects.first() self.assertTrue(ai_attachment.attachment.name.endswith("png")) self.assertEqual(0, ai_attachment.file_no) self.assertEqual(200, requests.get(ai_attachment.get_absolute_url()).status_code) # Assert product self.assertEqual(1, Product.objects.get(pk=p.pk).ai_pics_count)
def test_should_limit_file_count(self): p = ProductFactory.create(ai_pics_count=0) response = self.json_request( self.url + "?device_id=TEST-DEVICE-ID", data={ 'description': "test-description", 'product_id': p.pk, 'files_count': 50, 'file_ext': 'jpg', 'mime_type': 'image/jpeg', }, ) self.assertEqual(403, response.status_code) self.assertEqual("Forbidden", response.reason_phrase)
def test_should_return_200_when_polish_and_known_product(self): c = CompanyFactory.create( plCapital=100, plWorkers=0, plRnD=100, plRegistered=100, plNotGlobEnt=100, description="KOTEK", sources="KOTEK|BBBB", verified=True, is_friend=True, plCapital_notes="AA", plWorkers_notes="BBB", plRnD_notes="CCC", plRegistered_notes="DDD", plNotGlobEnt_notes="EEEE", ) p = ProductFactory.create(code=5900049011829, company=c, brand=None) response = self.json_request(self.url + "?device_id=TEST-DEVICE-ID&code=" + str(p.code)) self.assertEqual(200, response.status_code)
def test_code_with_multiple_company(self): current_ean = TEST_EAN13 company1 = CompanyFactory.create(name='test-company1', description='test-description') company2 = CompanyFactory.create(name='test-company2', description='test-description') product = ProductFactory.create(code=current_ean, company=company1, brand__company=company2) with mock.patch("pola.logic.get_by_code", return_value=product): response = get_result_from_code(current_ean) # TODO: Add support for multiple companies in this response expected_response = ( { 'altText': None, 'card_type': 'type_grey', 'code': '5900084231145', 'description': 'test-description', 'is_friend': False, 'name': company1.official_name, 'plCapital': None, 'plCapital_notes': None, 'plNotGlobEnt': None, 'plNotGlobEnt_notes': None, 'plRegistered': None, 'plRegistered_notes': None, 'plRnD': None, 'plRnD_notes': None, 'plScore': None, 'plWorkers': None, 'plWorkers_notes': None, 'product_id': product.id, 'report_button_text': 'Zgłoś', 'report_button_type': 'type_white', 'report_text': ('Zgłoś jeśli posiadasz bardziej aktualne dane na temat tego produktu'), 'sources': {}, }, {"was_590": True, "was_plScore": False, "was_verified": False}, product, ) self.maxDiff = None self.assertEqual(expected_response[0], response[0]) self.assertEqual(expected_response, response)
def test_should_limit_file_count(self): p = ProductFactory.create(ai_pics_count=0) response = self.json_request( self.url + "?device_id=TEST-DEVICE-ID", data={ 'product_id': p.pk, 'files_count': 50, 'file_ext': "png", "mime_type": 'image/jpeg', 'original_width': 1000, 'original_height': 2000, 'width': 100, 'height': 200, 'device_name': 'TEST-device', 'flash_used': True, 'was_portrait': False, }, ) self.assertEqual(403, response.status_code) self.assertEqual("Forbidden", response.reason_phrase)
def test_internal_code(self): prefix = "000" current_ean = prefix + TEST_EAN13[3:] product = ProductFactory.create(code=current_ean, company=None, brand=None) with mock.patch("pola.logic.get_by_code", return_value=product): response = get_result_from_code(current_ean) expected_response = ( { "altText": ( 'Zeskanowany kod jest wewnętrznym kodem sieci handlowej. Pola nie ' 'potrafi powiedzieć o nim nic więcej' ), "card_type": "type_white", "code": current_ean, "name": 'Kod wewnętrzny', "plCapital": None, "plCapital_notes": None, "plNotGlobEnt": None, "plNotGlobEnt_notes": None, "plRegistered": None, "plRegistered_notes": None, "plRnD": None, "plRnD_notes": None, "plScore": None, "plWorkers": None, "plWorkers_notes": None, "product_id": product.id, "report_button_text": "Zgłoś", "report_button_type": "type_white", "report_text": 'Zgłoś jeśli posiadasz bardziej aktualne dane na temat tego produktu', }, {"was_590": False, "was_plScore": False, "was_verified": False}, product, ) self.maxDiff = None self.assertEqual(expected_response[0], response[0]) self.assertEqual(expected_response, response)
def test_should_create_report(self): p = ProductFactory.create(ai_pics_count=0) response = self.json_request( self.url + "?device_id=TEST-DEVICE-ID", data={ 'description': "test-description", 'product_id': p.pk, 'files_count': 1, 'file_ext': 'jpg', 'mime_type': 'image/jpeg', }, ) self.assertEqual(200, response.status_code) response_data = response.json() self.assertEqual(len(response_data['signed_requests']), 1) signed_url: str = response_data['signed_requests'][0] self.assertTrue(signed_url.startswith("http://minio:9000")) # Valid signed URL response = requests.put( signed_url, data=_create_image(), headers={"x-amz-acl": "public-read", 'Content-Type': 'image/jpeg'} ) self.assertEqual(200, response.status_code, response.text) # Assert Report self.assertEqual(Report.objects.count(), 1) report: Report = Report.objects.get(pk=response_data['id']) self.assertEqual(p.pk, report.product_id) self.assertEqual("test-description", report.description) self.assertEqual("TEST-DEVICE-ID", report.client) self.assertEqual(Report.OPEN, report.status()) # Assert Attachment self.assertEqual(1, Attachment.objects.count()) report = Attachment.objects.first() self.assertTrue(report.attachment.name.endswith("jpg")) self.assertEqual(200, requests.get(report.get_absolute_url()).status_code)
def test_missing_company_and_book(self, prefix): current_ean = prefix + TEST_EAN13[3:] product = ProductFactory.create(code=current_ean, company=None, brand=None) with mock.patch("pola.logic.get_by_code", return_value=product): response = get_result_from_code(current_ean) expected_response = ( { "altText": ( 'Zeskanowany kod jest kodem ISBN/ISSN/ISMN dotyczącym książki, ' 'czasopisma lub albumu muzycznego. Wydawnictwa tego typu nie są ' 'aktualnie w obszarze zainteresowań Poli.' ), "card_type": "type_white", "code": current_ean, "name": "Kod ISBN/ISSN/ISMN", "plCapital": None, "plCapital_notes": None, "plNotGlobEnt": None, "plNotGlobEnt_notes": None, "plRegistered": None, "plRegistered_notes": None, "plRnD": None, "plRnD_notes": None, "plScore": None, "plWorkers": None, "plWorkers_notes": None, "product_id": product.id, "report_button_text": "Zgłoś", "report_button_type": "type_white", "report_text": 'To nie jest książka, czasopismo lub album muzyczny? Prosimy o zgłoszenie', }, {"was_590": False, "was_plScore": False, "was_verified": False}, product, ) self.maxDiff = None self.assertEqual(expected_response, response)
def test_missing_company_and_590(self, mock_get_by_code): product = ProductFactory.create(code=TEST_EAN13, company=None, brand=None) mock_get_by_code.return_value = product response = get_result_from_code(TEST_EAN13) expected_response = ( { "altText": ( "Każde skanowanie jest rejestrowane. Najczęściej skanowane firmy i produkty, " "których nie mamy jeszcze w bazie, są weryfikowane w pierwszej kolejności. " "Nie pobieramy przy tym żadnych informacji o użytkowniku.\n" "\n" "Jeśli chcesz zgłosić błąd lub wyrazić opinię, prosimy o kontakt: [email protected]" ), "card_type": "type_grey", "code": "5900084231145", "name": "Dziękujemy za użycie aplikacji Pola!", "plCapital": None, "plCapital_notes": None, "plNotGlobEnt": None, "plNotGlobEnt_notes": None, "plRegistered": None, "plRegistered_notes": None, "plRnD": None, "plRnD_notes": None, "plScore": None, "plWorkers": None, "plWorkers_notes": None, "product_id": product.id, "report_button_text": "Zgłoś", "report_button_type": "type_red", "report_text": "Bardzo prosimy o zgłoszenie nam tego produktu", }, {"was_590": True, "was_plScore": False, "was_verified": False}, product, ) self.assertEqual(expected_response, response)
def test_missing_company_and_wrong_country(self, prefix, country): current_ean = prefix + TEST_EAN13[3:] product = ProductFactory.create(code=current_ean, company=None, brand=None) with mock.patch("pola.logic.get_by_code", return_value=product): response = get_result_from_code(current_ean) expected_response = ( { "altText": ( 'Ten produkt został wyprodukowany przez zagraniczną firmę, której ' 'miejscem rejestracji jest: {}.'.format(country) ), "card_type": "type_grey", "code": current_ean, "name": f'Miejsce rejestracji: {country}', "plCapital": None, "plCapital_notes": None, "plNotGlobEnt": None, "plNotGlobEnt_notes": None, "plRegistered": None, "plRegistered_notes": None, "plRnD": None, "plRnD_notes": None, "plScore": 0, "plWorkers": None, "plWorkers_notes": None, "product_id": product.id, "report_button_text": "Zgłoś", "report_button_type": "type_white", "report_text": 'Zgłoś jeśli posiadasz bardziej aktualne dane na temat tego produktu', }, {"was_590": False, "was_plScore": False, "was_verified": False}, product, ) self.assertEqual(expected_response[0], response[0]) self.assertEqual(expected_response, response)