|
| 1 | +"""Unit tests for POST requests sent to api.widget_list API endpoint.""" |
| 2 | +from datetime import date, timedelta |
| 3 | +from http import HTTPStatus |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from tests.util import ( |
| 8 | + EMAIL, |
| 9 | + ADMIN_EMAIL, |
| 10 | + BAD_REQUEST, |
| 11 | + FORBIDDEN, |
| 12 | + DEFAULT_NAME, |
| 13 | + login_user, |
| 14 | + create_widget, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("widget_name", ["abc123", "widget-name", "new_widget1"]) |
| 19 | +def test_create_widget_valid_name(client, db, admin, widget_name): |
| 20 | + response = login_user(client, email=ADMIN_EMAIL) |
| 21 | + assert "access_token" in response.json |
| 22 | + access_token = response.json["access_token"] |
| 23 | + response = create_widget(client, access_token, widget_name=widget_name) |
| 24 | + assert response.status_code == HTTPStatus.CREATED |
| 25 | + assert "status" in response.json and response.json["status"] == "success" |
| 26 | + success = f"New widget added: {widget_name}." |
| 27 | + assert "message" in response.json and response.json["message"] == success |
| 28 | + location = f"http://localhost/api/v1/widgets/{widget_name}" |
| 29 | + assert "Location" in response.headers and response.headers["Location"] == location |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "deadline_str", |
| 34 | + [ |
| 35 | + date.today().strftime("%m/%d/%Y"), |
| 36 | + date.today().strftime("%Y-%m-%d"), |
| 37 | + (date.today() + timedelta(days=3)).strftime("%b %d %Y"), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_create_widget_valid_deadline(client, db, admin, deadline_str): |
| 41 | + response = login_user(client, email=ADMIN_EMAIL) |
| 42 | + assert "access_token" in response.json |
| 43 | + access_token = response.json["access_token"] |
| 44 | + response = create_widget(client, access_token, deadline_str=deadline_str) |
| 45 | + assert response.status_code == HTTPStatus.CREATED |
| 46 | + assert "status" in response.json and response.json["status"] == "success" |
| 47 | + success = f"New widget added: {DEFAULT_NAME}." |
| 48 | + assert "message" in response.json and response.json["message"] == success |
| 49 | + location = f"http://localhost/api/v1/widgets/{DEFAULT_NAME}" |
| 50 | + assert "Location" in response.headers and response.headers["Location"] == location |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "deadline_str", |
| 55 | + [ |
| 56 | + "1/1/1970", |
| 57 | + (date.today() - timedelta(days=3)).strftime("%Y-%m-%d"), |
| 58 | + "a long time ago, in a galaxy far, far away", |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_create_widget_invalid_deadline(client, db, admin, deadline_str): |
| 62 | + response = login_user(client, email=ADMIN_EMAIL) |
| 63 | + assert "access_token" in response.json |
| 64 | + access_token = response.json["access_token"] |
| 65 | + response = create_widget(client, access_token, deadline_str=deadline_str) |
| 66 | + assert response.status_code == HTTPStatus.BAD_REQUEST |
| 67 | + assert "message" in response.json and response.json["message"] == BAD_REQUEST |
| 68 | + assert "errors" in response.json and "deadline" in response.json["errors"] |
| 69 | + |
| 70 | + |
| 71 | +def test_create_widget_already_exists(client, db, admin): |
| 72 | + response = login_user(client, email=ADMIN_EMAIL) |
| 73 | + assert "access_token" in response.json |
| 74 | + access_token = response.json["access_token"] |
| 75 | + response = create_widget(client, access_token) |
| 76 | + assert response.status_code == HTTPStatus.CREATED |
| 77 | + response = create_widget(client, access_token) |
| 78 | + assert response.status_code == HTTPStatus.CONFLICT |
| 79 | + name_conflict = f"Widget name: {DEFAULT_NAME} already exists, must be unique." |
| 80 | + assert "message" in response.json and response.json["message"] == name_conflict |
| 81 | + |
| 82 | + |
| 83 | +def test_create_widget_no_admin_token(client, db, user): |
| 84 | + response = login_user(client, email=EMAIL) |
| 85 | + assert "access_token" in response.json |
| 86 | + access_token = response.json["access_token"] |
| 87 | + response = create_widget(client, access_token) |
| 88 | + assert response.status_code == HTTPStatus.FORBIDDEN |
| 89 | + assert "message" in response.json and response.json["message"] == FORBIDDEN |
0 commit comments