Exemplo n.º 1
0
def test_list_own_stories(client: client):
    # list own stories without auth
    response = client.get("/api/stories")
    assert_json_status(response, Unauthorized.code)

    # list own stories with auth
    response = client.get("/api/stories", headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)
    assert len(response.json) is 2
Exemplo n.º 2
0
def test_use_auth(client: client):
    # access auth'ed resource without auth
    response = client.get("/api/me")
    assert_json_status(response, Unauthorized.code)

    # access auth'ed resource with incorrect auth
    response = client.get("/api/me", headers=with_bad_auth_headers())
    assert_json_status(response, Unauthorized.code)

    # access auth'ed resource with correct auth
    response = client.get("/api/me", headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)
    assert response.json["username"] == CREDENTIALS_GOOD["username"]
Exemplo n.º 3
0
def test_list_user_stories(client: client):
    # list public stories (without auth)
    response = client.get("/api/stories/user/{0}".format(
        CREDENTIALS_GOOD["username"]))
    assert_json_status(response, OK_STATUS)
    for story in response.json:
        assert story["public"] is True

    # list all stories (with auth)
    response = client.get("/api/stories/user/{0}".format(
        CREDENTIALS_GOOD["username"]),
                          headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)
    def test_get_multiple_zones(self, client, initial_data, zone_data,
                                basic_auth_admin_headers):
        res = client.post("/api/v1/pdnsadmin/zones",
                          headers=basic_auth_admin_headers,
                          data=json.dumps(zone_data),
                          content_type="application/json")
        data = res.get_json(force=True)
        data['rrsets'] = []

        validate_zone(data)
        assert res.status_code == 201

        res = client.get("/api/v1/pdnsadmin/zones",
                         headers=basic_auth_admin_headers)
        data = res.get_json(force=True)
        fake_domain = namedtuple("Domain", data[0].keys())(*data[0].values())
        domain_schema = DomainSchema(many=True)

        json.dumps(domain_schema.dump([fake_domain]))
        assert res.status_code == 200

        zone_url_format = "/api/v1/pdnsadmin/zones/{0}"
        zone_url = zone_url_format.format(zone_data['name'].rstrip("."))
        res = client.delete(zone_url, headers=basic_auth_admin_headers)

        assert res.status_code == 204
Exemplo n.º 5
0
 def test_response_body(self, client):
     response_body = client.get('/rulesets').get_json()
     assert response_body == {
         'status': 400,
         'field': 'session_id',
         'error': 'required'
     }
Exemplo n.º 6
0
    def test_get_multiple_zones(
        self,
        client,
        common_data_mock,
        zone_data,
        admin_apikey
    ):
        with patch('app.blueprints.api.Domain') as mock_domain:
            test_domain = Domain(1, name=zone_data['name'].rstrip("."))
            mock_domain.query.all.return_value = [test_domain]

            res = client.get(
                "/api/v1/servers/localhost/zones",
                headers=admin_apikey
            )
            data = res.get_json(force=True)

            fake_domain = namedtuple(
                "Domain",
                data[0].keys()
            )(*data[0].values())
            domain_schema = DomainSchema(many=True)

            json.dumps(domain_schema.dump([fake_domain]))
            assert res.status_code == 200
 def test_empty_get(self, initial_apikey_data, client,
                    user_apikey_integration):
     res = client.get("/api/v1/servers/localhost/zones",
                      headers=user_apikey_integration)
     data = res.get_json(force=True)
     assert res.status_code == 200
     assert data == []
def test_user_can_access_home_page(client, configed_app):
    with configed_app.test_request_context():
        response = client.get("/home", follow_redirects=False)

    assert (
        response.status_code == 200
    ), f"Home page can not be accessed. Status code: {response.status_code}"
Exemplo n.º 9
0
def test_play_game(client: client):
    # first, check if the game exists
    response = client.get("/api/game/play?id={0}".format(VARS["game_id"]))
    assert_json_status(response, OK_STATUS)
    turn_username = response.json["data"]["turn"]
    assert turn_username in (c["username"]
                             for c in (CREDENTIALS_GOOD, CREDENTIALS_ALT))

    turn_credentials = from_username(turn_username)
    not_turn_credentials = other_user(credentials=turn_credentials)

    # try to play when it isn't our turn
    response = client.post(
        "/api/game/play",
        json={
            "id": VARS["game_id"],
            "move": "grapple"
        },
        headers=with_auth_headers(credentials=not_turn_credentials))
    assert_json_status(response, BadRequest.code)

    # make a valid play
    response = client.post(
        "/api/game/play",
        json={
            "id": VARS["game_id"],
            "move": "punch"
        },
        headers=with_auth_headers(credentials=turn_credentials))
    assert_json_status(response, OK_STATUS)
    assert response.json["data"]["turn"] == not_turn_credentials["username"]
Exemplo n.º 10
0
def test_data_monthly_endpoint_price_path(client):
    response = client.get('/cbeci/api/data/monthly/0.05')  # type: Response

    assert response_is_successful(response)
    assert response_has_data_list(response)
    assert response_data_item_has_keys(
        response, ['month', 'timestamp', 'value', 'cumulative_value'])
Exemplo n.º 11
0
def test_api_response(client):
    resp = client.get('/api/version')
    resp_json = json.loads(resp.data)
    assert 'status' in resp_json.keys()
    assert 'data' in resp_json.keys()
    assert resp_json['status'] == 'OK'
    assert resp_json['data'].get('version')
Exemplo n.º 12
0
def test_data_stats_endpoint_without_price(client):
    response = client.get('/cbeci/api/data/stats/')  # type: Response

    assert response_is_successful(response)
    assert b"Welcome to the CBECI API data endpoint. " \
        b"To get bitcoin electricity consumption estimate timeseries, " \
        b"specify electricity price parameter 'p' (in USD), for example /api/data/stats/?p=0.05" == response.data
Exemplo n.º 13
0
 def test_accounts_empty_get(
         self,
         client,
         initial_data,  # noqa: F811
         basic_auth_user_headers):  # noqa: F811
     res = client.get("/api/v1/pdnsadmin/accounts",
                      headers=basic_auth_user_headers)
     assert res.status_code == 401
Exemplo n.º 14
0
def test_download_endpoint_v1_1_1_download_monthly_data(client):
    response = client.get('/cbeci/api/v1.1.1/download/data/monthly')  # type: Response
    assert response_is_successful(response)
    assert response_is_csv(response)
    assert csv_has_headers(response, [
        'Month',
        'Monthly consumption, TWh',
        'Cumulative consumption, TWh',
    ], row_i=1)
Exemplo n.º 15
0
def test_feed_accessible_with_login(client, session):  # noqa
    u = make_user()
    session.add(u)
    session.commit()

    with client.session_transaction() as sess:
        sess['user_id'] = str(u.id)
    res = client.get(flask.url_for('routes.get_feed_route'))
    assert res.status_code == 200
Exemplo n.º 16
0
def test_data_endpoint_price_path(client):
    response = client.get('/cbeci/api/data/0.05')  # type: Response

    assert response_is_successful(response)
    assert response_has_data_list(response)
    assert response_data_item_has_keys(response, [
        'date', 'guess_consumption', 'max_consumption', 'min_consumption',
        'timestamp'
    ])
Exemplo n.º 17
0
def test_data_stats_endpoint_price_path(client):
    response = client.get('/cbeci/api/data/stats/0.05')  # type: Response

    assert response_is_successful(response)
    response_data = json.loads(response.data)
    assert item_has_keys(response_data, [
        'guess_consumption', 'guess_power', 'max_consumption', 'max_power',
        'min_consumption', 'min_power'
    ])
Exemplo n.º 18
0
 def test_accounts_empty_get(
         self,
         client,
         initial_data,  # noqa: F811
         basic_auth_admin_headers):  # noqa: F811
     res = client.get("/api/v1/pdnsadmin/accounts",
                      headers=basic_auth_admin_headers)
     data = res.get_json(force=True)
     assert res.status_code == 200
     assert data == []
Exemplo n.º 19
0
def test_get_should_return_user(client):
    res = client.get("/users/1")

    assert res.status_code == 200
    assert res.json["firstname"] == user1["firstname"]
    assert res.json["lastname"] == user1["lastname"]
    assert res.json["birthdate"] == user1["birthdate"].strftime("%Y-%m-%d")
    assert res.json["fiscalcode"] == user1["fiscalcode"]
    assert res.json["email"] == user1["email"]
    assert res.json["phonenumber"] == user1["phonenumber"]
Exemplo n.º 20
0
def test_get_should_not_return_operator(client):
    res = client.get("/operators/3")

    assert res.status_code == 404

    client.post(
        "/operators",
        json=operator_not_written,
    )

    res = client.get("/operators/3")

    assert res.status_code == 200
    assert res.json["firstname"] == operator_not_written["firstname"]
    assert res.json["lastname"] == operator_not_written["lastname"]
    assert res.json["birthdate"] == operator_not_written["birthdate"].strftime(
        "%Y-%m-%d")
    assert res.json["fiscalcode"] == operator_not_written["fiscalcode"]
    assert res.json["email"] == operator_not_written["email"]
    assert res.json["phonenumber"] == operator_not_written["phonenumber"]
Exemplo n.º 21
0
def test_play_story(client: client):
    # play public story (without auth)
    response = client.get("/api/story/{0}/play".format(
        VARS["story_public"]["id"]))
    assert response.status_code == OK_STATUS

    # play private story (without auth)
    response = client.get("/api/story/{0}/play".format(
        VARS["story_private"]["id"]))
    assert response.status_code == Unauthorized.code

    # play private story (with auth)
    response = client.get("/api/story/{0}/play".format(
        VARS["story_private"]["id"]),
                          headers=with_auth_headers())
    assert response.status_code == OK_STATUS

    # play non-existent story (without auth)
    response = client.get("/api/story/{0}/play".format("not_a_story"))
    assert response.status_code == NotFound.code
Exemplo n.º 22
0
def test_get_story_info(client: client):
    # get public story (without auth)
    response = client.get("/api/story/{0}".format(VARS["story_public"]["id"]))
    assert_json_status(response, OK_STATUS)
    assert response.json["id"] == VARS["story_public"]["id"]
    assert response.json["author"] == CREDENTIALS_GOOD["username"]

    # get private story (without auth)
    response = client.get("/api/story/{0}".format(VARS["story_private"]["id"]))
    assert_json_status(response, Unauthorized.code)

    # get private story (with auth)
    response = client.get("/api/story/{0}".format(VARS["story_private"]["id"]),
                          headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)
    assert response.json["id"] == VARS["story_private"]["id"]
    assert response.json["author"] == CREDENTIALS_GOOD["username"]

    # get non-existent story
    response = client.get("/api/story/{0}".format("not_a_story"))
    assert_json_status(response, NotFound.code)
Exemplo n.º 23
0
 def test_self_get(
         self,
         initial_data,
         client,
         test_user,  # noqa: F811
         basic_auth_user_headers):  # noqa: F811
     self.user = None
     res = client.get("/api/v1/pdnsadmin/users/{}".format(test_user),
                      headers=basic_auth_user_headers)
     data = res.get_json(force=True)
     assert res.status_code == 200
     assert len(data) == 1, data
     self.user = data
    def test_empty_get(self, client, common_data_mock,
                       basic_auth_admin_headers):
        with patch('app.blueprints.api.Domain') as mock_domain, \
             patch('app.lib.utils.requests.get') as mock_get:
            mock_domain.return_value.domains.return_value = []
            mock_domain.query.all.return_value = []
            mock_get.return_value.json.return_value = []
            mock_get.return_value.status_code = 200

            res = client.get("/api/v1/pdnsadmin/zones",
                             headers=basic_auth_admin_headers)
            data = res.get_json(force=True)
            assert res.status_code == 200
            assert data == []
Exemplo n.º 25
0
def test_should_trace_one_user_through_phonenumber(client, db):
    res = client.get(
        "/authorities/1/trace",
        json={"identifier": user1["phonenumber"], "duration": 15},
        follow_redirects=False,
    )

    date = res.json[0]["date"]
    user = res.json[0]["people"][0]

    assert date == datetime.date.today().strftime("%Y-%m-%d")
    assert user["email"] == user2["email"]
    # TODO user3
    assert res.status_code == 200
    def test_get_multiple_zones(self, client, common_data_mock, zone_data,
                                basic_auth_user_headers):
        test_domain = Domain(1, name=zone_data['name'].rstrip("."))
        self.mockk.get_domains.return_value = [test_domain]

        res = client.get("/api/v1/pdnsadmin/zones",
                         headers=basic_auth_user_headers)
        data = res.get_json(force=True)

        fake_domain = namedtuple("Domain", data[0].keys())(*data[0].values())
        domain_schema = DomainSchema(many=True)

        json.dumps(domain_schema.dump([fake_domain]))
        assert res.status_code == 200
Exemplo n.º 27
0
    def test_empty_get(self, client, common_data_mock, admin_apikey):
        with patch('powerdnsadmin.routes.api.Domain') as mock_domain, \
             patch('powerdnsadmin.lib.utils.requests.get') as mock_get:
            mock_domain.return_value.domains.return_value = []
            mock_domain.query.all.return_value = []
            mock_get.return_value.json.return_value = []
            mock_get.return_value.status_code = 200

            res = client.get("/api/v1/servers/pdnsadmin/zones",
                             headers=admin_apikey)

            data = res.get_json(force=True)
            assert res.status_code == 200
            assert data == []
Exemplo n.º 28
0
def test_download_endpoint_v1_1_1_download_data(client):
    response = client.get('/cbeci/api/v1.1.1/download/data')  # type: Response
    assert response_is_successful(response)
    assert response_is_csv(response)
    assert csv_has_headers(response, [
        'Timestamp',
        'Date and Time',
        'power MAX, GW',
        'power MIN, GW',
        'power GUESS, GW',
        'annualised consumption MAX, TWh',
        'annualised consumption MIN, TWh',
        'annualised consumption GUESS, TWh',
    ], row_i=1)
Exemplo n.º 29
0
def test_delete_story(client: client):
    # delete story (without auth)
    response = client.delete("/api/story/{0}".format(
        VARS["story_public"]["id"]))
    assert_json_status(response, Unauthorized.code)

    # delete story (with auth)
    response = client.delete("/api/story/{0}".format(
        VARS["story_public"]["id"]),
                             headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)

    # check if deleted story is really gone
    response = client.get("/api/story/{0}".format(VARS["story_public"]["id"]))
    assert_json_status(response, NotFound.code)
Exemplo n.º 30
0
def test_edit_story(client: client):
    # edit story (without auth)
    response = client.put("/api/story/{0}".format(VARS["story_private"]["id"]),
                          json={"public": True})
    assert_json_status(response, Unauthorized.code)

    # edit story (with auth)
    response = client.put("/api/story/{0}".format(VARS["story_private"]["id"]),
                          json={"public": True},
                          headers=with_auth_headers())
    assert_json_status(response, OK_STATUS)

    # check if story is now public
    response = client.get("/api/story/{0}".format(VARS["story_private"]["id"]))
    assert_json_status(response, OK_STATUS)
Exemplo n.º 31
0
def test_error_404(client):
    resp = client.get(utils.get_cryptogen_string())
    assert "<title>StarterWeppy-404</title>" in resp.data
Exemplo n.º 32
0
def test_account_page_access(client):
    resp = client.get('/account/login')
    assert "StarterWeppy | Account" in resp.data
Exemplo n.º 33
0
def test_users_page_access(client):
    resp = client.get('/users/')
    assert "StarterWeppy-403" in resp.data
Exemplo n.º 34
0
def test_maintenance_page(client):
    resp = client.get("/maintenance_check")
    assert "StarterWeppy | Maintenance" in resp.data
Exemplo n.º 35
0
def test_welcome_page_access(client):
    resp = client.get('/')
    assert 'Welcome to StarterWeppy' in resp.data