def test_invite_new_user_to_association(self, user, send_invitation_mail): user.email = "*****@*****.**" session = Client().session() association = Association(name='prancing ponies') session.add(association) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=association.association_id)) session.flush() response = self.app.post( '/association/{0}/invite_user'.format(association.association_id), data={'email': '*****@*****.**'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'ok', 'action': 'invited'}) invite = session.query(AssociationInvite).one() eq_(invite.email, '*****@*****.**') eq_(invite.association_id, association.association_id) send_invitation_mail.assert_called_once_with( '*****@*****.**', '*****@*****.**', 'prancing ponies', 'http://localhost/accept_invitation/{0}/'.format(invite.token))
def test_add_existing_user_to_association(self, user): session = Client().session() association = Association(name='prancing ponies') session.add(association) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=association.association_id)) other_user = User(email_verified_at=utcnow(), email='*****@*****.**') session.add(other_user) session.flush() response = self.app.post( '/association/{0}/invite_user'.format(association.association_id), data={'email': '*****@*****.**'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'ok', 'action': 'added'}) user_associations = session.query( UserAssociation.user_id, UserAssociation.association_id).\ all() eq_(user_associations, [ (user.user_id, association.association_id), (other_user.user_id, association.association_id), ])
def test_generate_token(self): session = Client().session() user = User() session.add(user) session.flush() token = EmailVerificationToken.generate(user) eq_(len(token.token), 32)
def test_generate(self): session = Client().session() association = Association(name='crazy train') session.add(association) session.flush() invite = AssociationInvite.generate(association.association_id, '*****@*****.**')
def test_no_such_lib_raises_keyerror(self, g): session = Client().session() association = Association(name="prancing ponies") session.add(association) session.flush() g.association_id = association.association_id radlibs.lib.load_lib('Loot')
def test_breadcrumb_for_an_association(self): session = Client().session() association = Association(name="Harpy") session.add(association) session.flush() with app.app_context(): crumb = breadcrumb_for(association) eq_(crumb, ('Harpy', 'http://localhost/association/{0}'.format( association.association_id)))
def test_accept_invite_prompts_for_login(self): session = Client().session() association = Association(name="tower of power") session.add(association) session.flush() invite = AssociationInvite.generate(association.association_id, '*****@*****.**') session.flush() response = self.app.get('/accept_invitation/{0}/'.format(invite.token)) eq_(response.status_code, 200) assert 'Please create an account' in response.data,\ "Response didn't prompt for login"
def register(): db_session = Client().session() user = User( email=request.form['email'], identifier=session['partial_user']['identifier']) db_session.add(user) db_session.flush() token = EmailVerificationToken.generate(user) send_verification_mail( user, url_for('verify_email', token=token.token, _external=True)) session['user'] = {'email': user.email, 'identifier': user.identifier} return redirect(request.form['redirect_uri'])
def test_hmac_auth__invalid_datetime_format(self): session = Client().session() user = User(api_key='hurfdurf') session.add(user) session.flush() response = self.app.post( '/test_authorization', data={'user_id': user.user_id, 'signature': 'johnhancock', 'time': 'beer:30', 'other_param': 'frabjous'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': 'not logged in'})
def test_bypass_login__existing_user(self): session = Client().session() user = User(identifier='http://www.facebook.com/itsme', email='*****@*****.**') session.add(user) session.flush() response = self.app.post('/login_bypass', data={ 'email': '*****@*****.**', 'identifier': 'http://www.facebook.com/itsme', }) eq_(response.status_code, 302) eq_(response.headers['Location'], 'http://localhost/')
def test_test_radlib_requires_login(self): session = Client().session() association = Association(name="somebody's private stuff") session.add(association) session.flush() response = self.app.post( '/association/{0}/test_radlib'.format(association.association_id), data={'rad': 'I ate some <Food>'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': 'login required'})
def create_association(self, user): session = Client().session() association = Association(name="Partytown") session.add(user) session.add(association) session.flush() user_association = UserAssociation( user_id=user.user_id, association_id=association.association_id) session.add(user_association) session.flush() return association.association_id
def test_add_new_rad__requires_correct_user(self, user): session = Client().session() other_user = User() association_id = self.create_association(other_user) lib = Lib(name="Song", association_id=association_id) session.add(lib) session.flush() response = self.app.post('/lib/{0}/rad/new'.format(lib.lib_id), data={'rad': '<Song_which_never_ends>'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': 'no such lib'})
def test_hmac_auth__user_has_no_api_key(self): session = Client().session() user = User() session.add(user) session.flush() time = datetime.datetime.utcnow().strftime('%Y%m%dT%H:%M:%S') signature = "mloop droop" response = self.app.post( '/test_authorization', data={'user_id': user.user_id, 'signature': signature, 'time': time, 'other_param': 'frabjous'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': 'not logged in'})
def test_accept_invite_while_unverified_verifies_email(self, user): user.email = '*****@*****.**' user.email_verified_at = None session = Client().session() association = Association(name="tower of power") session.add(association) session.flush() invite = AssociationInvite.generate(association.association_id, '*****@*****.**') session.flush() response = self.app.get('/accept_invitation/{0}/'.format(invite.token)) eq_(response.status_code, 302, response.data) del(user) user = session.query(User).one() assert user.email_verified_at, "Email wasn't verified"
def test_add_user_to_association_requires_correct_login(self, user): session = Client().session() association = Association(name='prancing ponies') session.add(association) session.flush() response = self.app.post( '/association/{0}/invite_user'.format(association.association_id), data={'email': '*****@*****.**'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': 'no such association'})
def create_association(): if not g.user: abort(401) name = request.form['name'] session = Client().session() association = Association(name=name) session.add(association) session.flush() user_association = UserAssociation( user_id=g.user.user_id, association_id=association.association_id) session.add(user_association) return redirect(url_for('manage_association', association_id=association.association_id))
def test_add_new_rad__requires_user(self): session = Client().session() association = Association(name='Partytown') session.add(association) session.flush() lib = Lib(name="Animal", association_id=association.association_id) session.add(lib) session.flush() response = self.app.post('/lib/{0}/rad/new'.format(lib.lib_id), data={'rad': 'what is happening'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': 'login required'})
def test_test_radlib_with_unknown_lib(self, user): session = Client().session() association = Association(name="pdx python") session.add(association) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=association.association_id)) response = self.app.post( '/association/{0}/test_radlib'.format(association.association_id), data={'rad': 'I ate some <Food>'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': "no such lib 'Food'"})
def test_invite__user_is_already_in_the_association(self, user): session = Client().session() watercooler = Association(name='watercooler') session.add(watercooler) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=watercooler.association_id)) session.flush() response = self.app.post( '/association/{0}/invite_user'.format(watercooler.association_id), data={'email': user.email}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': 'already in association'})
def test_create_new_lib__lib_already_exists(self, user): session = Client().session() association_id = self.create_association(user) lib = Lib(name="Rant", association_id=association_id) session.add(lib) session.flush() response = self.app.post( '/association/{0}/lib/new'.format(association_id), data={"name": "Rant"}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': 'lib already exists' })
def test_edit_rad(self, user): session = Client().session() association_id = self.create_association(user) lib = Lib(name="Band", association_id=association_id) session.add(lib) session.flush() rad = Rad(lib_id=lib.lib_id, created_by=user.user_id, rad='Huey Lewis and the Nws') session.add(rad) session.flush() response = self.app.post('/lib/rad/{0}/edit'.format(rad.rad_id), data={'rad': 'Huey Lewis and the News'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'ok'})
def test_new_rad_by_name__missing_params(self, user): session = Client().session() association_id = self.create_association(user) lib = Lib(name="Song", association_id=association_id) session.add(lib) session.flush() response = self.app.post('/lib/rad/new', data={ 'lib': 'Song', 'rad': 'Stairway to <Location>', }) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': "missing param 'association_id'" })
def test_add_new_rad_by_name__syntax_error(self, user): session = Client().session() association_id = self.create_association(user) lib = Lib(name="Song", association_id=association_id) session.add(lib) session.flush() response = self.app.post('/lib/rad/new', data={ 'association_id': association_id, 'lib': 'Song', 'rad': 'All you need is <Emotion', }) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': "parse error: Unexpected token '<' at line 1 " "character 17 of 'All you need is <Emotion'"})
def test_invite__invalid_email_address(self, user): session = Client().session() watercooler = Association(name='watercooler') session.add(watercooler) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=watercooler.association_id)) session.flush() response = self.app.post( '/association/{0}/invite_user'.format(watercooler.association_id), data={'email': 'not a valid email'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': "invalid email address 'not a valid email'"})
def test_verify_email(self): session = Client().session() user = User() session.add(user) session.flush() token = EmailVerificationToken.generate(user) response = self.app.get('/verify_email/{0}'.format(token.token)) eq_(response.status_code, 200, response.data) assert 'Thanks!' in response.data, 'response was rude' del(user) user = session.query(User).one() assert user.email_verified_at, "email wasn't verified!" tokens = session.query(EmailVerificationToken).all() eq_(tokens, [])
def test_test_radlib_with_syntactically_invalid_rad(self, user): session = Client().session() association = Association(name="pdx python") session.add(association) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=association.association_id)) response = self.app.post( '/association/{0}/test_radlib'.format(association.association_id), data={'rad': 'I ate some Food>'}) eq_(response.status_code, 200) body = json.loads(response.data) eq_(body, { 'status': 'error', 'error': "Unexpected token '>' at line 1 character 16 of " "'I ate some Food>'"})
def test_users_can_only_manage_their_own_associations(self, user): session = Client().session() other_user = User(email='*****@*****.**', identifier='facebook.com/imogenheap') session.add(other_user) association = Association(name="lalala") session.add(association) session.flush() user_association = UserAssociation( user_id=other_user.user_id, association_id=association.association_id) session.add(user_association) session.flush() response = self.app.get('/association/{0}'.format( association.association_id)) eq_(response.status_code, 404, response.data)
def test_invite_a_user_who_has_been_invited_to_that_association(self, user): session = Client().session() watercooler = Association(name='watercooler') session.add(watercooler) session.flush() session.add(UserAssociation(user_id=user.user_id, association_id=watercooler.association_id)) session.flush() AssociationInvite.generate(watercooler.association_id, '*****@*****.**') response = self.app.post( '/association/{0}/invite_user'.format(watercooler.association_id), data={'email': '*****@*****.**'}) eq_(response.status_code, 200, response.data) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': 'already invited'})
def test_add_rad__invalid_syntax(self, user): session = Client().session() association_id = self.create_association(user) lib = Lib(name="Artist", association_id=association_id) session.add(lib) session.flush() response = self.app.post('/lib/{0}/rad/new'.format(lib.lib_id), data={'rad': 'Ziggy Astral_body>dust'}) eq_(response.status_code, 200) rads = session.query(Rad).all() eq_(rads, []) body = json.loads(response.data) eq_(body, {'status': 'error', 'error': "parse error: Unexpected token '>' at line 1 " "character 18 of 'Ziggy Astral_body>dust'"})