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

feat: change owner of event #268

Merged
merged 20 commits into from Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/routers/event.py
Expand Up @@ -125,6 +125,31 @@ async def eventview(request: Request, event_id: int,
"messages": messages})


@router.post("/{event_id}/owner")
async def change_owner(request: Request, event_id: int,
db: Session = Depends(get_db)):
form = await request.form()
if 'username' not in form:
return RedirectResponse(router.url_path_for('eventview',
event_id=event_id),
status_code=status.HTTP_302_FOUND)
username = form['username']
user = db.query(User).filter_by(username=username).first()
try:
user_id = user.id
except AttributeError as e:
error_message = f"Username does not exist. {form['username']}"
logger.exception(str(e))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=error_message)
owner_to_update = {'owner_id': user_id}
_update_event(db, event_id, owner_to_update)
return RedirectResponse(router.url_path_for('eventview',
event_id=event_id),
status_code=status.HTTP_302_FOUND)


def by_id(db: Session, event_id: int) -> Event:
"""Get a single event by id"""
if not isinstance(db, Session):
Expand Down
4 changes: 2 additions & 2 deletions app/templates/base.html
Expand Up @@ -2,7 +2,7 @@
<html lang="en">

<head>
{% block head %}
{% block head %}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Expand Down Expand Up @@ -75,7 +75,7 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/js/bootstrap.bundle.min.js"
integrity="sha384-BOsAfwzjNJHrJ8cZidOg56tcQWfp6y72vEJ8xQ9w6Quywb24iOsW913URv1IS4GD"
crossorigin="anonymous"></script>

<script src="{{ url_for('static', path='/horoscope.js') }}"></script>
</body>

</html>
31 changes: 21 additions & 10 deletions app/templates/event/partials/view_event_details_tab.html
@@ -1,11 +1,11 @@
<div class="event_info_row title" style="border-bottom: 4px solid {{ event.color }}">
<div class="event_info_row_start">
<h1>{{ event.title }}</h1>
</div>
<div class="event_info_row_end">
<!-- <span class="icon">AVAILABILITY</span>-->
<!-- <span class="icon">PRIVACY</span>-->
</div>
<div class="event_info_row_start">
<h1>{{ event.title }}</h1>
</div>
<div class="event_info_row_end">
<!-- <span class="icon">AVAILABILITY</span>-->
<!-- <span class="icon">PRIVACY</span>-->
</div>
</div>
<div class="event_info_row">
<span class="icon">ICON</span>
Expand All @@ -25,13 +25,24 @@ <h1>{{ event.title }}</h1>
<address>{{ event.location }}</address>
<address>VC link<a href="{{ event.vc_link }}">VC URL</a></address>
</div>

{% if event.invitees %}
<form method="POST" action="/event/{{event.id}}/owner">
<div class="mb-3"></div>
<label for="username">{{ gettext('Choose user') }}</label>
<select id="username" name="username">
{% for username in event.invitees.split(',') %}
<option value="{{username}}">{{username}}</option>
{% endfor %}
</select>
<input type="submit" class="btn btn-primary" value="Change owner">
</form>
{% endif %}
<p class="event_info_row">
{{ event.content }}
{{event.owner.username}}
</p>

<div class="event_info_buttons_row event_info_row_end">
<!-- Buttons could and should be replaced with button-like anchors if need so -->
<button type="button">Duplicate</button>
<button type="button">Edit</button>
</div>
</div>
62 changes: 43 additions & 19 deletions tests/test_event.py
Expand Up @@ -11,6 +11,7 @@
from app.dependencies import get_db
from app.internal.utils import delete_instance
from app.main import app

from app.routers import event as evt


Expand Down Expand Up @@ -128,15 +129,9 @@ def test_eventedit(event_test_client):

def test_eventview_with_id(event_test_client, session, event):
event_id = event.id
event_details = [event.title, event.content, event.location,
event.vc_link, event.start,
event.end, event.color, event.category_id]
response = event_test_client.get(f"/event/{event_id}")
assert response.ok
assert b"View Event" in response.content
for event_detail in event_details:
assert str(event_detail).encode('utf-8') in response.content, \
f'{event_detail} not in view event page'


def test_create_event_with_default_availability(client, user, session):
Expand Down Expand Up @@ -339,9 +334,9 @@ def test_update_db_close(event):
data = {"title": "Problem connecting to db in func update_event", }
with pytest.raises(HTTPException):
assert (
evt.update_event(event_id=event.id, event=data,
db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
evt.update_event(event_id=event.id, event=data,
db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
)


Expand All @@ -360,11 +355,11 @@ def test_db_close_update(session, event):
data = {"title": "Problem connecting to db in func _update_event", }
with pytest.raises(HTTPException):
assert (
evt._update_event(
event_id=event.id,
event_to_update=data,
db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
evt._update_event(
event_id=event.id,
event_to_update=data,
db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
)


Expand All @@ -376,16 +371,16 @@ def test_no_connection_to_db_in_delete(event):
with pytest.raises(HTTPException):
response = evt.delete_event(event_id=1, db=None)
assert (
response.status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
response.status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
)


def test_no_connection_to_db_in_internal_deletion(event):
with pytest.raises(HTTPException):
assert (
evt._delete_event(event=event, db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
evt._delete_event(event=event, db=None).status_code ==
status.HTTP_500_INTERNAL_SERVER_ERROR
)


Expand All @@ -397,6 +392,33 @@ def test_successful_deletion(event_test_client, session, event):
db=session, event_id=1).content


def test_change_owner(client, event_test_client, user, session, event):
"""
Test change owner of an event
"""
event_id = event.id
event_details = [event.title, event.content, event.location, event.start,
event.end, event.color, event.category_id]
response = event_test_client.post(f"/event/{event_id}/owner",
data=None)
assert response.status_code == status.HTTP_302_FOUND
assert response.ok
assert b"View Event" not in response.content
for event_detail in event_details:
assert str(event_detail).encode('utf-8') not in response.content, \
f'{event_detail} not in view event page'
data = {'username': "worng_username"}
response = event_test_client.post(f"/event/{event_id}/owner",
data=data)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
assert b'Username does not exist.' in response.content
data = {'username': user.username}
response = event_test_client.post(f"/event/{event_id}/owner",
data=data)
assert response.ok
assert response.status_code == status.HTTP_302_FOUND


def test_deleting_an_event_does_not_exist(event_test_client, event):
response = event_test_client.delete("/event/2")
assert response.status_code == status.HTTP_404_NOT_FOUND
Expand Down Expand Up @@ -464,7 +486,9 @@ class TestApp:
"start": date_test_data[0],
"end": date_test_data[1],
"content": "Any Words",
"owner_id": 123}
"owner_id": 123,
'invitees': 'user1, user2'
}

@staticmethod
def test_get_db():
Expand Down