Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create hacknight list endpoint(fixes #123) #145

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from backend.extensions import api, db, mail, migrate, jwt
from backend.resources.auth import UserLogin, UserLogout
from backend.resources.contact import SendMessage
from backend.resources.hacknight import HacknightList
from backend.resources.participant import ParticipantsList


Expand Down Expand Up @@ -37,3 +38,4 @@ def initialize_extensions(app):
api.add_resource(UserLogout, "/auth/logout")
api.add_resource(SendMessage, "/send-email/")
api.add_resource(ParticipantsList, "/participants/")
api.add_resource(HacknightList, "/hacknights/")
14 changes: 14 additions & 0 deletions backend/resources/hacknight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from http import HTTPStatus

from flask_jwt_extended import jwt_required
from flask_restful import Resource

from backend.models import Hacknight
from backend.serializers.hacknight_serializer import hacknights_schema


class HacknightList(Resource):
@jwt_required
def get(self):
hacknights = hacknights_schema.dump(Hacknight.query.all())
return {'hacknights': hacknights}, HTTPStatus.OK
1 change: 0 additions & 1 deletion backend/resources/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ def get(self):
participants = participants_schema.dump(participants_list)
return {"participants": participants}, HTTPStatus.OK
return {"participants": []}, HTTPStatus.OK

2 changes: 2 additions & 0 deletions backend/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .participant_serializer import ParticipantSchema
from .hacknight_serializer import HacknightSchema
17 changes: 17 additions & 0 deletions backend/serializers/hacknight_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from marshmallow import Schema, fields


class HacknightSchema(Schema):
class Meta:
fields = ('id', 'date', 'participants')
dump_only = ('id',)

participants = fields.Nested(
'ParticipantSchema',
exclude=('name', 'lastname', 'email', 'hacknights', 'phone'),
many=True
)


hacknight_schema = HacknightSchema()
hacknights_schema = HacknightSchema(many=True, exclude=('participants',))
12 changes: 10 additions & 2 deletions backend/serializers/participant_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@

class ParticipantSchema(Schema):
class Meta:
fields = ('name', 'lastname', 'email', 'github', 'phone')
fields = (
'id', 'name', 'lastname', 'email', 'github', 'hacknights', 'phone'
)
dump_only = ('id', 'hacknights')

name = fields.Str(required=True, validate=[validate.Length(max=50)])
lastname = fields.Str(validate=[validate.Length(max=50)])
email = fields.Email(validate=[validate.Length(max=200)])
github = fields.Str(validate=[validate.Length(max=200)])
hacknights = fields.Nested(
'HacknightSchema',
exclude=('participants', ),
many=True
)
phone = fields.Str(validate=[validate.Length(max=13)])


participant_schema = ParticipantSchema()
participants_schema = ParticipantSchema(many=True)
participants_schema = ParticipantSchema(many=True, exclude=('hacknights',))
8 changes: 8 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from backend.app import create_app
from backend.extensions import db
from backend.factories import HacknightFactory
from backend.models import User


Expand Down Expand Up @@ -117,3 +118,10 @@ def new_msg():
"content": "Lorem Ipsum cos tam cos tam"
}
return msg


@pytest.fixture
def add_hacknights(app, _db):
for _ in range(10):
HacknightFactory.create()
_db.session.commit()
33 changes: 33 additions & 0 deletions backend/tests/test_hacknight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from http import HTTPStatus


def test_get_hacknights_when_logged_in(
client, access_token, add_hacknights
):
"""Test get list of hacknights for logged in user."""
rv = client.get(
'/hacknights/',
headers={'Authorization': 'Bearer {}'.format(access_token)}
)
response = rv.get_json()
assert rv.status_code == HTTPStatus.OK
assert len(response['hacknights']) == 10


def test_get_hacknights_with_empty_db(client, access_token):
"""Test get list of hacknights when no hacknight added to db."""
rv = client.get(
'/hacknights/',
headers={'Authorization': 'Bearer {}'.format(access_token)}
)
response = rv.get_json()
assert rv.status_code == HTTPStatus.OK
assert not response['hacknights']


def test_get_hacknights_unauthorized(client, add_hacknights):
"""Test get list of hacknights when user is not logged in."""
rv = client.get('/hacknights/')
response = rv.get_json()
assert rv.status_code == HTTPStatus.UNAUTHORIZED
assert response['msg'] == 'Missing Authorization Header'