def test_anon_uploads(client): with override_settings(SHUUP_CUSTOMER_INFORMATION_ALLOW_PICTURE_UPLOAD=True): tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup:media-upload"), data=dict({"file": data}), format="multipart") assert response.status_code == 302 # Anon uploads not allowed
def test_large_file(client, regular_user): client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) with override_settings(SHUUP_CUSTOMER_INFORMATION_ALLOW_PICTURE_UPLOAD=True): with override_settings(SHUUP_FRONT_MAX_UPLOAD_SIZE=10): tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup:media-upload"), data=dict({"file": data}), format="multipart") assert response.status_code == 400 data = json.loads(response.content.decode("utf-8")) assert "Maximum file size reached" in data["error"]
def test_upload_valid_image(client, rf, admin_user): assert File.objects.count() == 0 tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) #tmp_file.seek(0) client.login(username="******", password="******") with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup_admin:media.upload"), data=dict({"action": "upload", "file": data}), format="multipart" ) assert File.objects.count() == 1
def test_large_image(client, rf, admin_user): assert File.objects.count() == 0 with override_settings(SHUUP_MAX_UPLOAD_SIZE = 10): tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) #tmp_file.seek(0) client.login(username="******", password="******") with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup_admin:media.upload"), data=dict({"action": "upload", "file": data}), format="multipart" ) assert response.status_code == 400 data = json.loads(response.content.decode("utf-8")) assert "Maximum file size reached" in data["error"]["file"][0] assert File.objects.count() == 0
def test_uploads_allowed_setting(client, allow_image_uploads, regular_user): client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) with override_settings(SHUUP_CUSTOMER_INFORMATION_ALLOW_PICTURE_UPLOAD=allow_image_uploads): if allow_image_uploads: tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup:media-upload"), data=dict({"file": data}), format="multipart") assert response.status_code == 200 data = json.loads(response.content.decode("utf-8")) assert data["file"]["id"] else: tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) with open(tmp_file.name, 'rb') as data: response = client.post( reverse("shuup:media-upload"), data=dict({"file": data}), format="multipart") assert response.status_code == 403
def test_new_user_information_edit(allow_image_uploads): with override_settings( SHUUP_CUSTOMER_INFORMATION_ALLOW_PICTURE_UPLOAD=allow_image_uploads ): client = SmartClient() get_default_shop() # create new user user_password = "******" user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password=user_password, first_name="Niilo", last_name="Nyyppä", ) client.login(username=user.username, password=user_password) # make sure all information matches in form customer_edit_url = reverse("shuup:customer_edit") soup = client.soup(customer_edit_url) assert soup.find( attrs={"name": "contact-email"})["value"] == user.email assert soup.find( attrs={"name": "contact-first_name"})["value"] == user.first_name assert soup.find( attrs={"name": "contact-last_name"})["value"] == user.last_name # Test POSTing form = extract_form_fields(soup) new_email = "*****@*****.**" form["contact-email"] = new_email form["contact-country"] = "FI" for prefix in ("billing", "shipping"): form["%s-city" % prefix] = "test-city" form["%s-email" % prefix] = new_email form["%s-street" % prefix] = "test-street" form["%s-country" % prefix] = "FI" if allow_image_uploads: tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg') generate_image(120, 120).save(tmp_file) with open(tmp_file.name, 'rb') as data: response = client.post(reverse("shuup:media-upload"), data=dict({"file": data}), format="multipart") assert response.status_code == 200 data = json.loads(response.content.decode("utf-8")) file_id = data["file"]["id"] form["contact-picture"] = file_id response, soup = client.response_and_soup(customer_edit_url, form, "post") assert response.status_code == 302 user = get_user_model().objects.get(pk=user.pk) assert user.email == new_email contact = get_person_contact(user) if allow_image_uploads: assert contact.picture.id == file_id # Fetch page and check that the picture rendered there customer_edit_url = reverse("shuup:customer_edit") soup = client.soup(customer_edit_url) assert int( soup.find(attrs={"id": "id_contact-picture-dropzone"}) ["data-id"]) == file_id else: assert contact.picture is None