def test_todos(client, auth): response = client.get(app.reverse_uri("list_todos"), auth=auth) assert response.data == "[]" response = client.post(app.reverse_uri("create_todo"), auth=auth, json={ "description": "buy milk", "status": "todo", }) assert response.status_code == 201 todo = response.json() response = client.get( app.reverse_uri("get_todo", todo_id=todo["id"]), auth=auth, ) assert response.status_code == 200 assert response.json() == todo response = client.delete( app.reverse_uri("delete_todo", todo_id=todo["id"]), auth=auth, ) assert response.status_code == 204 response = client.get( app.reverse_uri("get_todo", todo_id=todo["id"]), auth=auth, ) assert response.status_code == 404
def test_chat_endpoint_broadcasts_messages_to_other_sockets(): with client.connect(app.reverse_uri("chat")) as alice, \ client.connect(app.reverse_uri("chat")) as bob, \ client.connect(app.reverse_uri("chat")) as eve: # noqa alice.send(TextMessage("hi!")) assert bob.receive(timeout=1).get_text() == "hi!" assert eve.receive(timeout=1).get_text() == "hi!" bob.send(TextMessage("hello!")) assert alice.receive(timeout=1).get_text() == "hello!" assert eve.receive(timeout=1).get_text() == "hello!"
def test_non_websocket_connections_to_websocket_endpoints_fail(): # Given that I have a websocket handler # When I request it without requesting a connection upgrade response = client.get(app.reverse_uri("echo")) # Then I should get back a 400 assert response.status_code == 400
def test_echo_endpoint_echos_messages_back(): with client.connect(app.reverse_uri("echo")) as sock: sock.send(TextMessage("hi!")) assert sock.receive(timeout=1).get_text() == "hi!" sock.send(BinaryMessage(b"hi!")) assert sock.receive(timeout=1).get_data() == b"hi!"
def test_can_create_todos(client: object) -> object: response = client.post( app.reverse_uri("create_todo"), json={"description": "example todo"}, ) assert response.status_code == 200 assert response.json()["description"] == "example todo"
def test_users_can_broadcast_messages(): # Given that I receiver_response = client.get( app.reverse_uri("receive_envelopes", username="******")) # When I broadcast a message broadcast_response = client.post(app.reverse_uri("send_envelope"), json={ "username": "******", "message": "Hi all!", }) # Then that request should succeed assert broadcast_response.status_code == 204 # And the receiver should get the new envelope envelope = json.loads(next(receiver_response.stream)) assert envelope == { "type": "envelope", "content": { "username": "******", "message": "Hi all!" } } # When I DM my receiver a message dm_response = client.post(app.reverse_uri("send_envelope"), json={ "recipient": "Jim", "username": "******", "message": "Hi Jim!", }) # Then that request should succeed assert dm_response.status_code == 204 # And the receiver should get the new envelope envelope = json.loads(next(receiver_response.stream)) assert envelope == { "type": "envelope", "content": { "username": "******", "message": "Hi Jim!" } }
def test_app_can_upload_pastes(): # When I submit a new paste response = client.post( app.reverse_uri("upload"), body=b"example", ) # Then I should get back a successful response assert response.status_code == 200 # When I try to get the paste paste_uri = response.json() paste_id = paste_uri.split("/")[-1] response = client.get(app.reverse_uri("get_paste", paste_id=paste_id)) # Then I should get back a successful response assert response.status_code == 200 assert response.data == "example"
def test_create_book_invalid_format(authorized_client): body = { "title": "Code Complete", "author": "Steve McConel", "cover": "data:image/png:base64,data:image/png;base64,iVBORw0KGgo...", "file": "iVBORw0KGgoweartQW==", "format": "jpg", } response = authorized_client.post(app.reverse_uri("create_book"), json=body) assert response.status_code == 400 assert "errors" in response.json() assert "format" in response.json()["errors"]
def test_create_book(authorized_client): body = { "title": "Code Complete", "author": "Steve McConel", "cover": "data:image/png:base64,data:image/png;base64,iVBORw0KGgo...", "file": "iVBORw0KGgoweartQW==", "format": "pdf", } with mock.patch("readit.books.views.Converter") as converter_mock: converter_mock.return_value.convert.return_value = ["1", "2", "3"] response = authorized_client.post(app.reverse_uri("create_book"), json=body) assert response.status_code == 200 assert "id" in response.json()
def test_bad_request(client): response = client.get(app.reverse_uri("hello", name="Jim", age="invalid")) assert response.status_code == 400 assert response.json() == {"age": "expected int value"}
def test_successful_request(client): response = client.get(app.reverse_uri("hello", name="Jim", age=24)) assert response.status_code == 200 assert response.json() == "Hello 24 year old named Jim!"
def test_index_returns_200(): resp = client.get(app.reverse_uri("health")) assert resp.status_code == 200 assert resp.json()['version'] == 'v0.1.0'
def test_index_not_allowed_get(): resp = client.get(app.reverse_uri("predict")) assert resp.status_code == 404
def test_index_not_allowed_post(): resp = client.post(app.reverse_uri("health")) assert resp.status_code == 404
def test_create_book_validation_test(authorized_client): response = authorized_client.post(app.reverse_uri("create_book"), json={"invalid": "field"}) assert response.status_code == 400
def test_list_books(authorized_client, books_factory): books = books_factory(3) resp = authorized_client.get(app.reverse_uri("list_books")) assert resp.status_code == 200 assert len(resp.json()) == len(books)