예제 #1
0
def test_create_octocat_already_exists(client, db, admin):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CREATED

    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CONFLICT

    name_conflict = f"Octocat name: {DEFAULT_CAT_NAME} already exists, must be unique."
    assert "message" in response.json and response.json["message"] == name_conflict
예제 #2
0
def test_update_octocat(client, db, admin):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CREATED

    response = update_octocat(
        client,
        access_token,
        octocat_name=DEFAULT_CAT_NAME,
        url=UPDATED_URL,
        age=UPDATED_AGE,
    )
    print(f"---------------{response.json}")
    assert response.status_code == HTTPStatus.OK

    response = retrieve_octocat(client,
                                access_token,
                                octocat_name=DEFAULT_CAT_NAME)
    assert response.status_code == HTTPStatus.OK
    assert "name" in response.json and response.json["name"] == DEFAULT_CAT_NAME
    assert "age" in response.json and response.json["age"] == UPDATED_AGE
    assert "url" in response.json and response.json["url"] == UPDATED_URL
예제 #3
0
def test_create_octocat_no_admin_token(client, db, user):
    response = login_user(client, email=EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.FORBIDDEN
    assert "message" in response.json and response.json["message"] == FORBIDDEN
예제 #4
0
def test_create_octocat_valid_name(client, db, admin, octocat_name):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token, octocat_name=octocat_name)
    assert response.status_code == HTTPStatus.CREATED
    assert "status" in response.json and response.json["status"] == "success"

    success = f"New octocat added: {octocat_name}."
    assert "message" in response.json and response.json["message"] == success

    location = f"http://localhost/api/v1/octocats/{octocat_name}"
    assert "Location" in response.headers and response.headers["Location"] == location
예제 #5
0
def test_delete_octocat_no_admin_token(client, db, admin, user):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CREATED

    response = login_user(client, email=EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = delete_octocat(client,
                              access_token,
                              octocat_name=DEFAULT_CAT_NAME)
    assert response.status_code == HTTPStatus.FORBIDDEN
    assert "message" in response.json and response.json["message"] == FORBIDDEN
예제 #6
0
def test_delete_octocat(client, db, admin):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CREATED

    response = delete_octocat(client,
                              access_token,
                              octocat_name=DEFAULT_CAT_NAME)
    assert response.status_code == HTTPStatus.NO_CONTENT

    response = retrieve_octocat(client,
                                access_token,
                                octocat_name=DEFAULT_CAT_NAME)
    assert response.status_code == HTTPStatus.NOT_FOUND
예제 #7
0
def test_retrieve_octocat_non_admin_user(client, db, admin, user):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = create_octocat(client, access_token)
    assert response.status_code == HTTPStatus.CREATED

    response = login_user(client, email=EMAIL)
    assert "access_token" in response.json

    access_token = response.json["access_token"]
    response = retrieve_octocat(client,
                                access_token,
                                octocat_name=DEFAULT_CAT_NAME)
    assert response.status_code == HTTPStatus.OK

    assert "name" in response.json and response.json["name"] == DEFAULT_CAT_NAME
    assert "url" in response.json and response.json["url"] == DEFAULT_URL
    assert "age" in response.json and response.json["age"] == DEFAULT_AGE
예제 #8
0
def test_retrieve_paginated_octocat_list(client, db, admin):
    response = login_user(client, email=ADMIN_EMAIL)
    assert "access_token" in response.json
    access_token = response.json["access_token"]

    # ADD SEVEN octocat INSTANCES TO DATABASE
    for i in range(0, len(NAMES)):
        response = create_octocat(
            client,
            access_token,
            octocat_name=NAMES[i],
            url=URLS[i],
            age=AGES[i],
        )
        assert response.status_code == HTTPStatus.CREATED

    # REQUEST PAGINATED LIST OF OCTOCATS: 5 PER PAGE, PAGE #1
    response = retrieve_octocat_list(client, access_token, page=1, per_page=5)
    assert response.status_code == HTTPStatus.OK

    # VERIFY PAGINATION ATTRIBUTES FOR PAGE #1
    assert "has_prev" in response.json and not response.json["has_prev"]
    assert "has_next" in response.json and response.json["has_next"]
    assert "page" in response.json and response.json["page"] == 1
    assert "total_pages" in response.json and response.json["total_pages"] == 2
    assert "items_per_page" in response.json and response.json[
        "items_per_page"] == 5
    assert "total_items" in response.json and response.json["total_items"] == 7
    assert "items" in response.json and len(response.json["items"]) == 5

    # VERIFY ATTRIBUTES OF OCTOCATS #1-5
    for i in range(0, len(response.json["items"])):
        item = response.json["items"][i]
        assert "name" in item and item["name"] == NAMES[i]
        assert "url" in item and item["url"] == URLS[i]
        assert "age" in item and item["age"] == AGES[i]

    # REQUEST PAGINATED LIST OF OCTOCATS: 5 PER PAGE, PAGE #2
    response = retrieve_octocat_list(client, access_token, page=2, per_page=5)
    assert response.status_code == HTTPStatus.OK

    # VERIFY PAGINATION ATTRIBUTES FOR PAGE #2
    assert "has_prev" in response.json and response.json["has_prev"]
    assert "has_next" in response.json and not response.json["has_next"]
    assert "page" in response.json and response.json["page"] == 2
    assert "total_pages" in response.json and response.json["total_pages"] == 2
    assert "items_per_page" in response.json and response.json[
        "items_per_page"] == 5
    assert "total_items" in response.json and response.json["total_items"] == 7
    assert "items" in response.json and len(response.json["items"]) == 2

    # VERIFY ATTRIBUTES OF OCTOCATS #6-7
    for i in range(5, response.json["total_items"]):
        item = response.json["items"][i - 5]
        assert "name" in item and item["name"] == NAMES[i]
        assert "url" in item and item["url"] == URLS[i]
        assert "age" in item and item["age"] == AGES[i]

    # REQUEST PAGINATED LIST OF OCTOCATS: 10 PER PAGE, PAGE #1
    response = retrieve_octocat_list(client, access_token, page=1, per_page=10)
    assert response.status_code == HTTPStatus.OK

    # VERIFY PAGINATION ATTRIBUTES FOR PAGE #1
    assert "has_prev" in response.json and not response.json["has_prev"]
    assert "has_next" in response.json and not response.json["has_next"]
    assert "page" in response.json and response.json["page"] == 1
    assert "total_pages" in response.json and response.json["total_pages"] == 1
    assert "items_per_page" in response.json and response.json[
        "items_per_page"] == 10
    assert "total_items" in response.json and response.json["total_items"] == 7
    assert "items" in response.json and len(response.json["items"]) == 7

    # VERIFY ATTRIBUTES OF OCTOCATS #1-7
    for i in range(0, len(response.json["items"])):
        item = response.json["items"][i]
        assert "name" in item and item["name"] == NAMES[i]
        assert "url" in item and item["url"] == URLS[i]
        assert "age" in item and item["age"] == AGES[i]

    # REQUEST PAGINATED LIST OF OCTOCATS: DEFAULT PARAMETERS
    response = retrieve_octocat_list(client, access_token)
    assert response.status_code == HTTPStatus.OK

    # VERIFY PAGINATION ATTRIBUTES FOR PAGE #1
    assert "has_prev" in response.json and not response.json["has_prev"]
    assert "has_next" in response.json and not response.json["has_next"]
    assert "page" in response.json and response.json["page"] == 1
    assert "total_pages" in response.json and response.json["total_pages"] == 1
    assert "items_per_page" in response.json and response.json[
        "items_per_page"] == 10
    assert "total_items" in response.json and response.json["total_items"] == 7
    assert "items" in response.json and len(response.json["items"]) == 7

    # VERIFY ATTRIBUTES OF OCTOCATS #1-7
    for i in range(0, len(response.json["items"])):
        item = response.json["items"][i]
        assert "name" in item and item["name"] == NAMES[i]
        assert "url" in item and item["url"] == URLS[i]
        assert "age" in item and item["age"] == AGES[i]