def test_multiple_elements(self) -> None: """Test get list with multiple elements.""" pod_names = ("first_pod", "second_pod") podcasts = [] tracks = [] for pod_name in pod_names: pod = Podcast(name=pod_name) pod.save() podcasts.append(pod) track_titles = ("first_track", "second_track") for track_title, podcast in zip(track_titles, podcasts): track = Track(podcast=podcast, title=track_title) track.save() tracks.append(track) endpoint = get_endpoint(f"track/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json()["results"] for elem, track in zip(data, tracks): assert elem["title"] == track.title endpoint = get_endpoint(f"track/podcast/{podcasts[0].id}/") res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json()["results"] assert len(data) == 1 assert data[0]["title"] == tracks[0].title
def test_create_update_podcast(self, create_user_auth, podcast_data) -> None: """Test create a new podcast and update it.""" res = create_user_auth token = res.json()["token"] endpoint = get_endpoint(f"podcast/") requests = APIClient() requests.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") res = requests.post(endpoint, json.dumps(podcast_data), content_type="application/json") assert res.status_code == status.HTTP_201_CREATED res = requests.get(endpoint, content_type="application/json") assert podcast_data["name"] == res.data["results"][0]["name"] podcast_id = res.data["results"][0]["id"] endpoint = get_endpoint(f"podcast/{podcast_id}/") new_name = "foobar" podcast_data["name"] = new_name requests.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") res = requests.put(endpoint, json.dumps(podcast_data), content_type="application/json") assert res.status_code == status.HTTP_200_OK res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK assert res.data["name"] == new_name
def test_new_user_invalid_json(self) -> None: """Test creating a new user with invalid json data.""" endpoint = get_endpoint("identify/") requests = APIClient() res = requests.post(endpoint, "foo", content_type="application/json") assert res.status_code == status.HTTP_400_BAD_REQUEST
def test_empty_list(self) -> None: """Test get empty list view.""" endpoint = get_endpoint(f"podcast/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json()["results"] assert data == []
def test_id(self, podcast_data, create_podcast) -> None: """Test id.""" pod = create_podcast endpoint = get_endpoint(f"podcast/{pod.id}/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json() assert pod.id == data["id"]
def test_not_found(self) -> None: """Test id not found.""" id = "9383837272" endpoint = get_endpoint(f"podcast/{id}/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_404_NOT_FOUND data = res.json() assert id in data
def test_id(self, create_track: Track) -> None: """Test id.""" track = create_track endpoint = get_endpoint(f"track/{track.id}/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json() assert data["id"] == track.id
def test_multiple_elements(self) -> None: """Test get list with multiple elements.""" pod_names = ("first_pod", "second_pod") for pod_name in pod_names: Podcast(name=pod_name).save() endpoint = get_endpoint(f"podcast/") requests = APIClient() res = requests.get(endpoint, content_type="application/json") assert res.status_code == status.HTTP_200_OK data = res.json()["results"] for elem, name in zip(data, pod_names): assert name == elem["name"]
def test_new_user_missing_fields(self) -> None: """Test creating a new user with missing fields.""" missing_fiels = ("password", "email") body = {"username": "******"} endpoint = get_endpoint("identify/") requests = APIClient() res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_400_BAD_REQUEST data = res.json() for missed_field in missing_fiels: assert missed_field in data
def test_new_user_auth(self) -> None: """Test creating a new user and re-auth.""" body = { "username": "******", "password": "******", "email": "*****@*****.**" } endpoint = get_endpoint("identify/") requests = APIClient() res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_201_CREATED data = res.json() assert "token" in data token = data["token"] endpoint = get_endpoint("auth/") res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_201_CREATED assert token.split(".")[:1] == res.data["token"].split(".")[:1] # missing user body2 = dict(body) del body2["username"] res = requests.post(endpoint, json.dumps(body2), content_type="application/json") assert res.status_code == status.HTTP_400_BAD_REQUEST # test wrong password body["password"] = "******" res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_400_BAD_REQUEST
def test_new_user(self) -> None: """Test creating a new user.""" body = { "username": "******", "password": "******", "email": "*****@*****.**" } endpoint = get_endpoint("identify/") requests = APIClient() res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_201_CREATED assert "token" in res.json() # try to treate the same user again res = requests.post(endpoint, json.dumps(body), content_type="application/json") assert res.status_code == status.HTTP_400_BAD_REQUEST assert "username" in res.json()