136 lines
4.6 KiB
Python
136 lines
4.6 KiB
Python
from django.contrib.auth.models import User
|
|
import datetime
|
|
import pytest
|
|
from webtest import TestApp
|
|
|
|
from chrono.wsgi import application
|
|
|
|
from chrono.agendas.models import Agenda, Event, Booking
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
@pytest.fixture
|
|
def admin_user():
|
|
try:
|
|
user = User.objects.get(username='admin')
|
|
except User.DoesNotExist:
|
|
user = User.objects.create_superuser('admin', email=None, password='admin')
|
|
return user
|
|
|
|
def login(app, username='admin', password='admin'):
|
|
login_page = app.get('/login/')
|
|
login_form = login_page.forms[0]
|
|
login_form['username'] = username
|
|
login_form['password'] = password
|
|
resp = login_form.submit()
|
|
assert resp.status_int == 302
|
|
return app
|
|
|
|
def test_unlogged_access(app):
|
|
# connect while not being logged in
|
|
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
|
|
|
def test_access(app, admin_user):
|
|
app = login(app)
|
|
resp = app.get('/manage/', status=200)
|
|
assert '<h2>Agendas</h2>' in resp.body
|
|
assert "This site doesn't have any agenda yet." in resp.body
|
|
|
|
def test_logout(app, admin_user):
|
|
app = login(app)
|
|
app.get('/logout/')
|
|
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
|
|
|
def test_menu_json(app, admin_user):
|
|
app = login(app)
|
|
resp = app.get('/manage/menu.json', status=200)
|
|
assert resp.json[0]['url'] == 'http://localhost:80/manage/'
|
|
assert resp.json[0]['label'] == 'Agendas'
|
|
resp2 = app.get('/manage/menu.json?callback=Q', status=200)
|
|
assert resp2.body == 'Q(%s);' % resp.body
|
|
|
|
def test_add_agenda(app, admin_user):
|
|
app = login(app)
|
|
resp = app.get('/manage/', status=200)
|
|
resp = resp.click('New')
|
|
resp.form['label'] = 'Foo bar'
|
|
resp = resp.form.submit()
|
|
agenda = Agenda.objects.get(label='Foo bar')
|
|
assert resp.location == 'http://localhost:80/manage/agendas/%s/' % agenda.id
|
|
resp = resp.follow()
|
|
assert '<h2>Agenda - Foo bar</h2>' in resp.body
|
|
|
|
def test_rename_agenda(app, admin_user):
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
app = login(app)
|
|
resp = app.get('/manage/', status=200)
|
|
resp = resp.click('Foo bar')
|
|
resp = resp.click('Rename')
|
|
assert resp.form['label'].value == 'Foo bar'
|
|
resp.form['label'] = 'Foo baz'
|
|
resp = resp.form.submit()
|
|
assert resp.location == 'http://localhost:80/manage/agendas/%s/' % agenda.id
|
|
resp = resp.follow()
|
|
assert '<h2>Agenda - Foo baz</h2>' in resp.body
|
|
|
|
def test_delete_agenda(app, admin_user):
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
app = login(app)
|
|
resp = app.get('/manage/', status=200)
|
|
resp = resp.click('Foo bar')
|
|
resp = resp.click('Delete')
|
|
resp = resp.form.submit()
|
|
assert resp.location == 'http://localhost:80/manage/'
|
|
resp = resp.follow()
|
|
assert not 'Foo bar' in resp.body
|
|
|
|
def test_add_event(app, admin_user):
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
app = login(app)
|
|
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
|
assert "This agenda doesn't have any event yet." in resp.body
|
|
resp = resp.click('New Event')
|
|
resp.form['start_datetime'] = '2016-02-15 17:00'
|
|
resp.form['places'] = 10
|
|
resp = resp.form.submit()
|
|
resp = resp.follow()
|
|
event = Event.objects.get(places=10)
|
|
assert not "This agenda doesn't have any event yet." in resp.body
|
|
assert '/manage/events/%s/' % event.id in resp.body
|
|
assert 'Feb. 15, 2016, 5 p.m.' in resp.body
|
|
assert '10 places' in resp.body
|
|
|
|
def test_edit_event(app, admin_user):
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
event = Event(start_datetime=datetime.datetime(2016, 2, 15, 17, 0),
|
|
places=20, agenda=agenda)
|
|
event.save()
|
|
app = login(app)
|
|
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
|
resp = resp.click('Feb. 15, 2016, 5 p.m.')
|
|
assert resp.form['start_datetime'].value == '15/02/2016 17:00'
|
|
resp.form['start_datetime'] = '2016-02-16 17:00'
|
|
resp.form['places'] = 20
|
|
resp = resp.form.submit()
|
|
resp = resp.follow()
|
|
assert '/manage/events/%s/' % event.id in resp.body
|
|
assert 'Feb. 16, 2016, 5 p.m.' in resp.body
|
|
assert '20 places' in resp.body
|
|
|
|
def test_booked_places(app, admin_user):
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
event = Event(start_datetime=datetime.datetime(2016, 2, 15, 17, 0),
|
|
places=10, agenda=agenda)
|
|
event.save()
|
|
Booking(event=event).save()
|
|
Booking(event=event).save()
|
|
app = login(app)
|
|
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
|
assert '10 places' in resp.body
|
|
assert '2 booked places' in resp.body
|