Skip to content

Commit

Permalink
Adding init github action and Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dotpep committed Feb 29, 2024
1 parent 7abc5fb commit ad94ffc
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 32 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Build and Deploy Code

on: [push, pull_request]

jobs:
my_first_job:
runs-on: ubuntu-20.04
steps:
- name: pulling git repo
uses: actions/checkout@v2
- name: say hi to user
run: echo "hello user!"
6 changes: 4 additions & 2 deletions tests/posts/test_all_posts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fastapi import status

from app import schemas


Expand Down Expand Up @@ -39,9 +41,9 @@ def test_get_posts_list(authorized_client, test_posts):
# assert validated_posts[i].Post.owner_id == test_posts[i].owner_id


assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK


def test_unauthorized_user_get_all_posts(client, test_posts):
res = client.get('/posts')
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
7 changes: 4 additions & 3 deletions tests/posts/test_create_post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from fastapi import status

from app import schemas

Expand All @@ -21,7 +22,7 @@ def test_create_post(authorized_client, test_user, test_posts, title, content, p
assert validated_created_post.published == published

assert validated_created_post.owner_id == test_user['id']
assert res.status_code == 201
assert res.status_code == status.HTTP_201_CREATED


def test_create_post_default_published_true(authorized_client, test_user, test_posts):
Expand All @@ -35,7 +36,7 @@ def test_create_post_default_published_true(authorized_client, test_user, test_p
validated_created_post = schemas.Post(**post)

assert validated_created_post.published == True
assert res.status_code == 201
assert res.status_code == status.HTTP_201_CREATED

assert validated_created_post.title == title
assert validated_created_post.content == content
Expand All @@ -44,4 +45,4 @@ def test_create_post_default_published_true(authorized_client, test_user, test_p
def test_unauthorized_user_create_post(client, test_user, test_posts):
request_data = {"title": "post title created unauthorized user", "content": "some content"}
res = client.post('/posts', json=request_data)
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
11 changes: 7 additions & 4 deletions tests/posts/test_delete_post.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from fastapi import status


def test_user_successfully_delete_post(authorized_client, test_user, test_posts):
post_id = test_posts[0].id
res = authorized_client.delete(f'/posts/{post_id}')
assert res.status_code == 204
assert res.status_code == status.HTTP_204_NO_CONTENT


def test_delete_post_non_exist(authorized_client, test_user, test_posts):
post_id = 9999
res = authorized_client.delete(f'/posts/{post_id}')
assert res.status_code == 404
assert res.status_code == status.HTTP_404_NOT_FOUND


def test_delete_other_user_post(authorized_client, test_user, test_posts):
another_user_post_id = test_posts[3].id
res = authorized_client.delete(f'/posts/{another_user_post_id}')
assert res.status_code == 403
assert res.status_code == status.HTTP_403_FORBIDDEN


def test_unauthorized_user_delete_post(client, test_user, test_posts):
post_id = test_posts[0].id
res = client.delete(f'/posts/{post_id}')
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
6 changes: 4 additions & 2 deletions tests/posts/test_last_post.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fastapi import status

from app import schemas


Expand All @@ -16,9 +18,9 @@ def test_get_last_post(authorized_client, test_posts):
assert validated_post.Post.title == test_post.title
assert validated_post.Post.content == test_post.content

assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK


def test_unauthorized_user_get_last_post(client, test_posts):
res = client.get(f'/posts/latest')
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
8 changes: 5 additions & 3 deletions tests/posts/test_one_post.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fastapi import status

from app import schemas


Expand All @@ -15,16 +17,16 @@ def test_get_one_post_detail(authorized_client, test_posts):
assert validated_post.Post.content == test_post.content
assert validated_post.Post.owner_id == test_post.owner_id

assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK


def test_unauthorized_user_get_one_post_detail(client, test_posts):
post_id = test_posts[0].id
res = client.get(f'/posts/{post_id}')
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED


def test_get_one_post_not_exist(authorized_client, test_posts):
post_id = 9999
res = authorized_client.get(f'/posts/{post_id}')
assert res.status_code == 404
assert res.status_code == status.HTTP_404_NOT_FOUND
9 changes: 5 additions & 4 deletions tests/posts/test_update_post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from fastapi import status

from app import schemas

Expand Down Expand Up @@ -26,7 +27,7 @@ def test_user_successfully_update_post(authorized_client, test_user, test_posts,
assert validated_updated_post.published == test_post.published
assert validated_updated_post.owner_id == test_user['id']

assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK



Expand All @@ -37,7 +38,7 @@ def test_update_post_non_exist(authorized_client, test_user, test_posts):
"published": False}
res = authorized_client.put(f'/posts/{post_id}', json=request_data)

assert res.status_code == 404
assert res.status_code == status.HTTP_404_NOT_FOUND


@pytest.mark.parametrize('title, content, published', [
Expand All @@ -57,7 +58,7 @@ def test_update_other_user_post(authorized_client, test_user, test_posts, title,
assert test_post.content != content
assert test_post.published != published

assert res.status_code == 403
assert res.status_code == status.HTTP_403_FORBIDDEN


def test_unauthorized_user_update_post(client, test_user, test_posts):
Expand All @@ -67,4 +68,4 @@ def test_unauthorized_user_update_post(client, test_user, test_posts):
"published": False}

res = client.put(f'/posts/{post_id}', json=request_data)
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
15 changes: 8 additions & 7 deletions tests/users/test_login_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from jose import jwt
from fastapi import status

from app.config import settings
from app import schemas
Expand All @@ -18,16 +19,16 @@ def test_login_user(client, test_user):
assert user_id == test_user['id']
assert login_resp.access_token == user['access_token']
assert login_resp.token_type == 'bearer'
assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK


@pytest.mark.parametrize("email, password, status_code", [
('[email protected]', 'password1234', 403),
('[email protected]', 'wrongpassword1234', 403),
('[email protected]', 'wrongpassword1234', 403),
(None, 'password1234', 422),
('[email protected]', None, 422),
(None, None, 422),
('[email protected]', 'password1234', status.HTTP_403_FORBIDDEN),
('[email protected]', 'wrongpassword1234', status.HTTP_403_FORBIDDEN),
('[email protected]', 'wrongpassword1234', status.HTTP_403_FORBIDDEN),
(None, 'password1234', status.HTTP_422_UNPROCESSABLE_ENTITY),
('[email protected]', None, status.HTTP_422_UNPROCESSABLE_ENTITY),
(None, None, status.HTTP_422_UNPROCESSABLE_ENTITY),
])
def test_incorrect_login(client, email, password, status_code):
request_data = {'username': email, 'password': password}
Expand Down
4 changes: 3 additions & 1 deletion tests/users/test_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fastapi import status

from app import schemas


Expand All @@ -9,7 +11,7 @@ def test_create_user(client):

validated_new_user = schemas.User(**user)

assert res.status_code == 201
assert res.status_code == status.HTTP_201_CREATED
assert validated_new_user.email == request_data['email']

#data = res.json()
Expand Down
10 changes: 6 additions & 4 deletions tests/votes/test_add_votes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fastapi import status

from app import models


Expand Down Expand Up @@ -27,7 +29,7 @@ def test_vote_on_post(authorized_client, test_user, test_posts, session):
assert db_user_id == query_vote.user_id

assert vote['message'] == 'successfully added vote'
assert res.status_code == 201
assert res.status_code == status.HTTP_201_CREATED


def test_vote_twice_post(authorized_client, test_posts, test_vote, test_user):
Expand All @@ -41,7 +43,7 @@ def test_vote_twice_post(authorized_client, test_posts, test_vote, test_user):
vote = res.json()

assert vote['detail'] == f"user {user_id=} has already voted on post {post_id=}"
assert res.status_code == 409
assert res.status_code == status.HTTP_409_CONFLICT



Expand All @@ -54,7 +56,7 @@ def test_vote_non_exist_post(authorized_client, test_posts):
vote = res.json()

assert vote['detail'] == f"post with {post_id=} was not found"
assert res.status_code == 404
assert res.status_code == status.HTTP_404_NOT_FOUND


def test_unauthorized_user_vote_on_post(client, test_posts):
Expand All @@ -63,4 +65,4 @@ def test_unauthorized_user_vote_on_post(client, test_posts):
request_data = {"post_id": post_id, "is_voted": is_vote}

res = client.post('/votes', json=request_data)
assert res.status_code == 401
assert res.status_code == status.HTTP_401_UNAUTHORIZED
7 changes: 5 additions & 2 deletions tests/votes/test_delete_votes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from fastapi import status


def test_successfully_delete_vote(authorized_client, test_posts, test_vote):
post_id = test_posts[3].id
is_vote = False # if false then delete vote
Expand All @@ -7,7 +10,7 @@ def test_successfully_delete_vote(authorized_client, test_posts, test_vote):
vote = res.json()

assert vote['message'] == 'successfully deleted vote'
assert res.status_code == 201
assert res.status_code == status.HTTP_201_CREATED


def test_delete_non_exist_vote(authorized_client, test_posts):
Expand All @@ -19,4 +22,4 @@ def test_delete_non_exist_vote(authorized_client, test_posts):
vote = res.json()

assert vote['detail'] == 'vote does not exists'
assert res.status_code == 404
assert res.status_code == status.HTTP_404_NOT_FOUND

0 comments on commit ad94ffc

Please sign in to comment.