def test_login_fail(app, monkeypatch): client = app.test_client() with app.app_context(): storage = MemoryStorage() monkeypatch.setattr(app.blueprints['google'], "storage", storage) with patch('juleol.db.Tastings') as MockTastings: test_tasting = db.Tastings() test_tasting.year = 2000 MockTastings.query.all.return_value = [test_tasting] ret = client.post("/login", data={'year': '2000'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/login/google' storage.set(app.blueprints['google'], {'access_token': 'fake-token'}) ret = client.post("/login", data={'year': '2000'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/login' ret = client.post("/login", data={'year': '2001'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/' ret = client.get('/') assert b'Invalid year' in ret.data with patch('juleol.db.Participants') as MockParticipants: MockParticipants.query.filter.return_value.filter.return_value.first.return_value = None ret = client.post("/login", data={'year': '2000'}) assert ret.status_code == 302 ret = client.get("/login") assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/' ret = client.get("/") assert b'No user with email [email protected] registered for year 2000' in ret.data
def test_update_tasting(admin_client): with patch('juleol.db.Tastings') as TastingsMock: with patch('juleol.db.db.session') as SessionMock: test_tasting = db.Tastings() test_tasting.year = 2000 test_tasting.locked = False TastingsMock.query.filter.return_value.first.return_value = None ret = admin_client.put('/admin/tasting/2001', data={"locked": "true"}) assert ret.status_code == 404 assert b'Invalid year' in ret.data TastingsMock.query.filter.return_value.first.return_value = test_tasting ret = admin_client.put('/admin/tasting/2000', data={"invalid": "123"}) assert ret.status_code == 400 assert b'Invalid argument' in ret.data ret = admin_client.put('/admin/tasting/2000', data={"locked": "true"}) assert ret.status_code == 200 assert test_tasting.locked == True assert SessionMock.mock_calls[0][0] == 'add' assert SessionMock.mock_calls[0][1] == (test_tasting, ) assert SessionMock.mock_calls[1][0] == 'commit' SessionMock.reset_mock() SessionMock.commit.side_effect = exc.SQLAlchemyError() ret = admin_client.put('/admin/tasting/2000', data={"locked": "true"}) assert ret.status_code == 500 assert b'Error updating tasting' in ret.data assert SessionMock.mock_calls[2][0] == 'rollback'
def test_index(client): with patch('juleol.db.Tastings') as MockTastings: test_tasting = db.Tastings() test_tasting.year = 2000 MockTastings.query.all.return_value = [test_tasting] ret = client.get('/') assert ret.status_code == 200 assert b'/result/2000' in ret.data assert b'Enter rating' not in ret.data
def test_admin_base(admin_client): with patch('juleol.db.Tastings') as MockTastings: test_tasting = db.Tastings() test_tasting.year = 2000 MockTastings.query.all.return_value = [test_tasting] ret = admin_client.get('/admin/') assert ret.status_code == 200 assert b'<li class="list-group-item list-group-item-secondary"><a class="text-primary" href="/admin/2000">2000</a></li>' in ret.data # noqa: E501
def test_update_participant(admin_client): with patch('juleol.db.Tastings') as TastingsMock: test_tasting = db.Tastings() test_tasting.year = 2000 TastingsMock.query.filter.return_value.first.return_value = test_tasting TastingsMock.query.filter.return_value.first.return_value = None ret = admin_client.post('/admin/2000/participant/1') assert ret.status_code == 302 ret = admin_client.get('/admin/') assert b'Invalid year' in ret.data TastingsMock.query.filter.return_value.first.return_value = test_tasting with patch('juleol.db.Participants') as ParticipantsMock: ParticipantsMock.query.filter.return_value.filter.return_value.first.return_value = None ret = admin_client.post('/admin/2000/participant/1') assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid participant' in ret.data test_participant = db.Participants() test_participant.tasting = test_tasting ParticipantsMock.query.filter.return_value.filter.return_value.first.return_value = test_participant ret = admin_client.post('/admin/2000/participant/1') assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid form data' in ret.data ret = admin_client.post('/admin/2000/participant/1', data={'email': 'email-invalid'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid form data' in ret.data with patch('juleol.db.db.session') as SessionMock: ret = admin_client.post( '/admin/2000/participant/1', data={'email': '*****@*****.**'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' assert test_participant.email == '*****@*****.**' assert SessionMock.mock_calls[0][0] == 'add' assert SessionMock.mock_calls[0][1] == (test_participant, ) assert SessionMock.mock_calls[1][0] == 'commit' SessionMock.reset_mock() SessionMock.commit.side_effect = exc.SQLAlchemyError() ret = admin_client.post('/admin/2000/participant/1', data={'email': '*****@*****.**'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Error updating email' in ret.data assert SessionMock.mock_calls[2][0] == 'rollback'
def test_year(admin_client): with patch('juleol.db.Tastings') as TastingsMock: test_tasting = db.Tastings() test_tasting.year = 2000 TastingsMock.query.filter.return_value.first.return_value = test_tasting ret = admin_client.get('/admin/2000') assert ret.status_code == 200 TastingsMock.query.filter.return_value.first.return_value = None ret = admin_client.get('/admin/2000') assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/' ret = admin_client.get('/admin/') assert b'Invalid year' in ret.data
def test_add_tasting(admin_client): with patch('juleol.db.Tastings') as TastingsMock: with patch('juleol.db.Beers') as BeersMock: with patch('juleol.db.db.session') as SessionMock: ret = admin_client.post('/admin/', data={ 'year': 2000, 'beers': 2 }) assert ret.status_code == 200 expected = [({'year': 2000, 'locked': False}, )] assert TastingsMock.call_args_list == expected expected = [({ 'number': 1, 'name': 'Unrevealed 1', 'tasting': db.Tastings(year=2000) }, ), ({ 'number': 2, 'name': 'Unrevealed 2', 'tasting': db.Tastings(year=2000) }, )] assert BeersMock.call_args_list == expected assert SessionMock.mock_calls[0][0] == 'add' assert SessionMock.mock_calls[0][1] == (db.Tastings( year=2000), ) assert SessionMock.mock_calls[1][0] == 'add' assert SessionMock.mock_calls[1][1] == (db.Beers( number=1, name='Unrevealed 1', tasting=db.Tastings(year=2000)), ) assert SessionMock.mock_calls[2][0] == 'add' assert SessionMock.mock_calls[2][1] == (db.Beers( number=2, name='Unrevealed 2', tasting=db.Tastings(year=2000)), ) assert SessionMock.mock_calls[3][0] == 'commit'
def test_login_session_corrupted(app): client = app.test_client() with client.session_transaction() as session: session['user_id'] = 5 with patch('juleol.db.Tastings') as MockTastings: with patch('juleol.db.Participants') as MockParticipants: test_tasting = db.Tastings() test_tasting.year = 2000 MockTastings.query.filter.return_value.filter.return_value.first.return_value = test_tasting MockParticipants.query.filter.return_value.first.return_value = None ret = client.get("/rate/2000") assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/' ret = client.get("/") assert b'Invalid user' in ret.data
def test_login_no_session(app): client = app.test_client() with patch('juleol.db.Tastings') as MockTastings: test_tasting = db.Tastings() test_tasting.year = 2000 MockTastings.query.all.return_value = [test_tasting] ret = client.get("/login") assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/' ret = client.get("/") assert b'No year selected' in ret.data ret = client.post("/login", data={'year': '2000'}) ret = client.get("/login") assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/login/google'
def admin_index(): form = TastingForm(request.form) if request.method == "POST" and form.validate(): try: tasting = db.Tastings(year=form.year.data, locked=False) db.db.session.add(tasting) for i in range(1, form.beers.data + 1): new_beer = db.Beers(tasting=tasting, number=i, name="Unrevealed {}".format(i)) db.db.session.add(new_beer) db.db.session.commit() flash("Tasting for year {} created".format(form.year.data)) except exc.SQLAlchemyError as e: db.db.session.rollback() current_app.logger.error("Error creating tasting: {}".format(e)) flash("Error creating tasting for year {}".format(form.year.data), 'error') tastings = db.Tastings.query.all() return render_template('admin.html', tastings=tastings, form=form)
def test_create_note(admin_client): with patch('juleol.db.Tastings') as TastingsMock: test_tasting = db.Tastings() test_tasting.year = 2000 TastingsMock.query.filter.return_value.first.return_value = test_tasting TastingsMock.query.filter.return_value.first.return_value = None ret = admin_client.post('/admin/2000/note') assert ret.status_code == 302 ret = admin_client.get('/admin/') assert b'Invalid year' in ret.data TastingsMock.query.filter.return_value.first.return_value = test_tasting with patch('juleol.db.Notes') as NotesMock: ret = admin_client.post('/admin/2000/note') assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid form data' in ret.data with patch('juleol.db.db.session') as SessionMock: ret = admin_client.post('/admin/2000/note', data={'note': 'test'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' assert NotesMock.mock_calls[0][2]['note'] == 'test' assert NotesMock.mock_calls[0][2]['tasting'] == test_tasting assert SessionMock.mock_calls[0][0] == 'add' assert SessionMock.mock_calls[0][1] == (db.Notes( note='test', tasting=test_tasting), ) assert SessionMock.mock_calls[1][0] == 'commit' SessionMock.reset_mock() SessionMock.commit.side_effect = exc.SQLAlchemyError() ret = admin_client.post('/admin/2000/note', data={'note': 'test'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Error creating note' in ret.data assert SessionMock.mock_calls[2][0] == 'rollback'
def test_create_participant(admin_client): with patch('juleol.db.Tastings') as TastingsMock: TastingsMock.query.filter.return_value.first.return_value = None ret = admin_client.post('/admin/2000/participant') assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/' ret = admin_client.get('/admin/') assert b'Invalid year' in ret.data test_tasting = db.Tastings() test_tasting.year = 2000 test_beer = db.Beers() test_beer.name = 'test beer' test_beer.number = 1 test_tasting.beers = [test_beer] test_tasting.participants = [] TastingsMock.query.filter.return_value.first.return_value = test_tasting ret = admin_client.post('/admin/2000/participant', data={'name': 'test'}) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid form data' in ret.data ret = admin_client.post('/admin/2000/participant', data={ 'name': 'test', 'email': 'email-invalid' }) assert ret.status_code == 302 assert ret.headers['Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Invalid form data' in ret.data with patch('juleol.db.Beers') as BeersMock: # noqa: F841 with patch('juleol.db.Participants') as ParticpantsMock: with patch('juleol.db.ScoreTaste') as MockScoreTaste: with patch('juleol.db.ScoreAftertaste' ) as MockScoreAftertaste: with patch('juleol.db.ScoreLook') as MockScoreLook: with patch( 'juleol.db.ScoreSmell') as MockScoreSmell: with patch('juleol.db.ScoreXmas' ) as MockScoreXmas: with patch('juleol.db.db.session' ) as SessionMock: ret = admin_client.post( '/admin/2000/participant', data={ 'name': 'test', 'email': '*****@*****.**' }) assert ret.status_code == 302 assert ret.headers[ 'Location'] == 'http://localhost/admin/2000' assert ParticpantsMock.mock_calls[0][ 2]['tasting'] == test_tasting assert ParticpantsMock.mock_calls[0][ 2]['name'] == 'test' assert ParticpantsMock.mock_calls[0][ 2]['email'] == '*****@*****.**' assert MockScoreLook.mock_calls[0][2][ 'tasting'] == test_tasting assert MockScoreLook.mock_calls[0][2][ 'beer'] == test_beer assert MockScoreSmell.mock_calls[0][2][ 'tasting'] == test_tasting assert MockScoreSmell.mock_calls[0][2][ 'beer'] == test_beer assert MockScoreTaste.mock_calls[0][2][ 'tasting'] == test_tasting assert MockScoreTaste.mock_calls[0][2][ 'beer'] == test_beer assert MockScoreAftertaste.mock_calls[ 0][2]['tasting'] == test_tasting assert MockScoreAftertaste.mock_calls[ 0][2]['beer'] == test_beer assert MockScoreXmas.mock_calls[0][2][ 'beer'] == test_beer assert MockScoreXmas.mock_calls[0][2][ 'tasting'] == test_tasting assert SessionMock.mock_calls[0][ 0] == 'add' assert SessionMock.mock_calls[0][ 1] == (db.Participants( name='test', email='*****@*****.**', tasting=test_tasting), ) assert SessionMock.mock_calls[1][ 0] == 'add' assert SessionMock.mock_calls[1][ 1] == (db.ScoreLook( tasting=test_tasting, beer=test_beer, participant=db.Participants( name='test', email='*****@*****.**', tasting=test_tasting)), ) assert SessionMock.mock_calls[2][ 0] == 'add' assert SessionMock.mock_calls[2][ 1] == (db.ScoreSmell( tasting=test_tasting, beer=test_beer, participant=db.Participants( name='test', email='*****@*****.**', tasting=test_tasting)), ) assert SessionMock.mock_calls[3][ 0] == 'add' assert SessionMock.mock_calls[3][ 1] == (db.ScoreTaste( tasting=test_tasting, beer=test_beer, participant=db.Participants( name='test', email='*****@*****.**', tasting=test_tasting)), ) assert SessionMock.mock_calls[4][ 0] == 'add' assert SessionMock.mock_calls[4][ 1] == (db.ScoreAftertaste( tasting=test_tasting, beer=test_beer, participant=db.Participants( name='test', email='*****@*****.**', tasting=test_tasting)), ) assert SessionMock.mock_calls[5][ 0] == 'add' assert SessionMock.mock_calls[5][ 1] == (db.ScoreXmas( tasting=test_tasting, beer=test_beer, participant=db.Participants( name='test', email='*****@*****.**', tasting=test_tasting)), ) assert SessionMock.mock_calls[6][ 0] == 'commit' SessionMock.reset_mock() SessionMock.commit.side_effect = exc.SQLAlchemyError( ) ret = admin_client.post( '/admin/2000/participant', data={ 'name': 'test', 'email': '*****@*****.**' }) assert ret.status_code == 302 assert ret.headers[ 'Location'] == 'http://localhost/admin/2000' ret = admin_client.get('/admin/2000') assert b'Error creating participant' in ret.data assert SessionMock.mock_calls[7][ 0] == 'rollback'