def test_subdomain_index(self, client, current_user): """User can list all of their subdomains""" sub = subdomain.SubdomainFactory.create(user=current_user) res3 = client.get("/subdomains") assert_valid_schema(res3.get_json(), "subdomains.json") assert str(sub.id) in values(res3.get_json(), "data/*/id")
def test_box_open_with_config(self, client, current_user, session): """User can open a box when providing a config they own""" conf = config.ConfigFactory(user=current_user) session.add(conf) session.flush() res = client.post( "/boxes", json={ "data": { "type": "box", "attributes": { "sshKey": "i-am-a-lousy-public-key" }, "relationships": { "config": { "data": { "type": "config", "id": str(conf.id) } } }, } }, ) assert res.status_code == 201 assert len(values(res.get_json(), "data/id")) == 1 assert_valid_schema(res.get_data(), "box.json") assert Box.query.filter_by(user=current_user).count() == 1
def test_tunnel_open_with_subdomain(self, client, current_user, session): """User can open a tunnel when providing a subdomain they own""" sub = subdomain.ReservedSubdomainFactory(user=current_user, name="testtunnelsubdomain") session.add(sub) session.flush() res = client.post( "/tunnels", json={ "data": { "type": "tunnel", "attributes": { "port": ["http"], "sshKey": "i-am-a-lousy-public-key", }, "relationships": { "subdomain": { "data": { "type": "subdomain", "id": str(sub.id) } } }, } }, ) assert res.status_code == 201 assert len(values(res.get_json(), "data/id")) == 1 assert_valid_schema(res.get_data(), "tunnel.json") assert Tunnel.query.filter_by(user=current_user).count() == 1
def test_subdomain_get(self, client, current_user): """User can get a single subdomain""" sub = subdomain.SubdomainFactory.create(user=current_user) res3 = client.get(f"/subdomains/{sub.id}") assert_valid_schema(res3.get_json(), "subdomain.json") assert str(sub.id) in values(res3.get_json(), "data/id")
def test_registration_contract(payload, response_status, schema): # GIVEN data = payload response = requests.post(url=registration['url'], data=data) json_data = response.json() # THEN assert response.status_code == response_status assert_valid_schema(json_data, schema)
def test_box_index(self, client, current_user, session): """User can list all of their boxes""" test_box = box.BoxFactory(config__user=current_user) session.add(test_box) session.flush() res3 = client.get("/boxes") assert_valid_schema(res3.get_json(), "boxes.json") assert str(test_box.id) in values(res3.get_json(), "data/*/id")
def test_get_box(self, client, current_user, session): """User can get a single box""" test_box = box.BoxFactory(config__user=current_user) session.add(test_box) session.flush() res3 = client.get(f"/boxes/{test_box.id}") assert_valid_schema(res3.get_json(), "box.json") assert str(test_box.id) in values(res3.get_json(), "data/id")
def test_get_tunnel(self, client, current_user, session): """User can get a single tunnel""" tun = tunnel.TunnelFactory(subdomain__user=current_user) session.add(tun) session.flush() res3 = client.get(f"/tunnels/{tun.id}") assert_valid_schema(res3.get_json(), "tunnel.json") assert str(tun.id) in values(res3.get_json(), "data/id")
def test_tunnel_index(self, client, current_user, session): """User can list all of their tunnels""" tun = tunnel.TunnelFactory(subdomain__user=current_user) session.add(tun) session.flush() res3 = client.get("/tunnels") assert_valid_schema(res3.get_json(), "tunnels.json") assert str(tun.id) in values(res3.get_json(), "data/*/id")
def test_get_search(): get_search_request = config.BASEURL + config.SearchResource + config.BengaluruCityQuery search_response = requests.get(get_search_request, headers={ 'Content-Type': 'application/json', 'user-key': '31f6ec0e0eed0b5624414a8229146b01' }) assert search_response.status_code == 200, 'status code is not equal to 200' search_response_json = json.loads(search_response.content) assert_valid_schema(search_response_json, 'SearchResponseSchema.json')
def test_get_establishments(): get_establishments_request = config.BASEURL + config.EstablishmentsResource + config.BengaluruCityIdQuery establishments_response = requests.get( get_establishments_request, headers={ 'Content-Type': 'application/json', 'user-key': '31f6ec0e0eed0b5624414a8229146b01' }) assert establishments_response.status_code == 200, 'status code is not equal to 200' establishments_response_json = json.loads(establishments_response.content) assert_valid_schema(establishments_response_json, 'EstablishmentsResponseSchema.json')
def test_get_users(req, username): """ Get username and validate JSON schema :param req: :param username: :return: """ all_users = req.get_all_users().json() user_id = [x['id'] for x in all_users if x['username'] == username][0] r = req.get_user_by_id(user_id) assert_that(r.status_code).is_equal_to(200) assert_valid_schema(r.json(), '{}.json'.format(username))
def test_get_reviews(): get_reviews_request = config.BASEURL + config.ReviewsResource + config.ResIdQuery reviews_response = requests.get(get_reviews_request, headers={ 'Content-Type': 'application/json', 'user-key': '31f6ec0e0eed0b5624414a8229146b01' }) assert reviews_response.status_code == 200, 'status code is not equal to 200' reviews_response_json = json.loads(reviews_response.content) assert_valid_schema(reviews_response_json, 'ReviewsResponseSchema.json')
def test_get_location_details(): get_location_details_request = config.BASEURL + config.LocationDetailsResource + config.BengaluruEntityIdQuery + \ '&' + config.BengaluruEntityTypeQuery location_details_response = requests.get( get_location_details_request, headers={ 'Content-Type': 'application/json', 'user-key': '31f6ec0e0eed0b5624414a8229146b01' }) assert location_details_response.status_code == 200, 'status code is not equal to 200' location_details_response_json = json.loads( location_details_response.content) assert_valid_schema(location_details_response_json, 'LocationDetailsResponseSchema.json')
def test_subdomain_filter(self, client, session, current_user): """Can filter a subdomain using JSON-API compliant filters""" sub1 = subdomain.ReservedSubdomainFactory(user=current_user, name="submarine") sub2 = subdomain.ReservedSubdomainFactory(user=current_user, name="sublime") session.add(sub1, sub2) session.flush() res = client.get(f"/subdomains?filter[name]=submarine") assert_valid_schema(res.get_json(), "subdomains.json") assert str(sub1.id) in values(res.get_json(), "data/*/id") assert str(sub2.id) not in values(res.get_json(), "data/*/id")
def test_box_filter_by_config_name(self, client, session, current_user): """Can filter a config using JSON-API compliant filters""" conf1 = config.ConfigFactory(user=current_user, name="sub-sandwich") conf2 = config.ConfigFactory(user=current_user, name="subscription") test_box1 = box.BoxFactory(config=conf1) test_box2 = box.BoxFactory(config=conf2) session.add(test_box1, test_box2) session.flush() res = client.get(f"/boxes?filter[config][name]=sub-sandwich") assert_valid_schema(res.get_json(), "boxes.json") assert str(test_box1.id) in values(res.get_json(), "data/*/id") assert str(test_box2.id) not in values(res.get_json(), "data/*/id")
def test_box_open_without_config(self, client, current_user, session): """User can open a box without providing a config""" res = client.post( "/boxes", json={ "data": { "type": "box", "attributes": { "sshKey": "i-am-lousy-public-key" }, } }, ) assert res.status_code == 201 assert_valid_schema(res.get_data(), "box.json") assert Box.query.filter_by(user=current_user).count() == 1
def test_tunnel_filter_by_subdomain_name(self, client, session, current_user): """Can filter a subdomain using JSON-API compliant filters""" sub1 = subdomain.ReservedSubdomainFactory(user=current_user, name="sub-sandwich") sub2 = subdomain.ReservedSubdomainFactory(user=current_user, name="subscription") tun1 = tunnel.TunnelFactory(subdomain=sub1) tun2 = tunnel.TunnelFactory(subdomain=sub2) session.add(tun1, tun2) session.flush() res = client.get(f"/tunnels?filter[subdomain][name]=sub-sandwich") assert_valid_schema(res.get_json(), "tunnels.json") assert str(tun1.id) in values(res.get_json(), "data/*/id") assert str(tun2.id) not in values(res.get_json(), "data/*/id")
def test_tunnel_open_with_multiple_ports(self, mock_get_unused_subdomain, client, current_user, session): """User can open a tunnel with multiple ports""" res = client.post( "/tunnels", json={ "data": { "type": "tunnel", "attributes": { "port": ["tcp", "http", "tcp"], "sshKey": "ssh-rsa AAAA\n", }, } }, ) assert res.status_code == 201 assert_valid_schema(res.get_data(), "tunnel.json") assert Tunnel.query.filter_by(user=current_user).count() == 1
def test_subdomain_reserve(self, client, current_user): """User can reserve a subdomain""" pre_subdomain_count = Subdomain.query.filter_by( user_id=current_user.id).count() res = client.post( "/subdomains", json={ "data": { "type": "subdomain", "attributes": { "name": "test" } } }, ) post_subdomain_count = Subdomain.query.filter_by( user_id=current_user.id).count() assert_valid_schema(res.get_data(), "subdomain.json") assert post_subdomain_count > pre_subdomain_count
def test_empty_subdomain_index(self, client): """User can list all of their subdomains when empty""" res = client.get("/subdomains") assert_valid_schema(res.get_json(), "subdomains.json")
def test_empty_box_index(self, client): """Correct response for empty box list""" res = client.get("/boxes") assert_valid_schema(res.get_json(), "boxes.json")
def test_empty_tunnel_index(self, client): """Correct response for empty tunnel list""" res = client.get("/tunnels") assert_valid_schema(res.get_json(), "tunnels.json")
def test_home_page(test_client): response = test_client.get('/') assert response.status_code == 200 json_data = json.loads(response.data.decode('utf-8')) assert_valid_schema(json_data, 'home.json')
def test_get_user(self, current_user, client): """Returns an access denied when old uuid is used in token""" res = client.get("/account") assert_valid_schema(res.get_data(), "user.json")