def test_add_provider(self): user = User(**self.userdata) user.add_provider(**self.authdata) auth = Auth.for_provider(self.authdata['provider'], self.authdata['provider_id']) self.assertEqual(Auth.query.count(), 1) self.assertEqual(auth.user, user)
def test_add_provider_conflict(self): auth = Auth(**self.authdata) user_a = User(**self.userdata) user_b = User(name='Hubble Bubbs') auth.user = user_a db.session.add(auth) db.session.commit() self.assertRaises(AuthExistsForUserException, user_b.add_provider, **self.authdata)
def test_get_current_user_authenticated(self): user = User(active=True, **self.userdata) self.db.session.add(user) self.db.session.commit() r = self.client.post('/test_login', data={'id': 1}) r = self.client.get('/user') self.assertEqual(r.status_code, 200)
def test_merge_prefer_merger(self): user_a = User(**self.userdata) user_b = User(name='Hubble Bubbs') auth_a = Auth(**self.authdata) auth_b = Auth(self.authdata['provider'], '8ahf81', '98kn32kafo2') auth_a.user = user_a auth_b.user = user_b for obj in [user_a, user_b, auth_a, auth_b]: db.session.add(obj) db.session.commit() user_a.merge(user_b) self.assertEqual(len(user_a.auths.all()), 1) self.assertEqual(user_a.auths.first().provider_id, self.authdata['provider_id'])
def test_merge(self): user_a = User(**self.userdata) user_b = User(name='Hubble Bubbs') auth_a = Auth(**self.authdata) auth_b = Auth('twitter', '8ahf81', '98kn32kafo2') auth_a.user = user_a auth_b.user = user_b for obj in [user_a, user_b, auth_a, auth_b]: db.session.add(obj) db.session.commit() self.assertEqual(User.query.count(), 2) user_a.merge(user_b) self.assertEqual(len(user_a.auths.all()), 2) self.assertEqual(User.query.count(), 1)
def _process_user(provider, provider_id, token, secret, userdata): """ Processes a user to either login or add a new authentication to an existing user. """ # If there is already an authenticated user, # add this provider to that user. if current_user.is_authenticated: try: current_user.add_provider(provider, provider_id, token, secret, update=True) # Conflict - this added authentication already exists but is # associated with another user. except AuthExistsForUserException as e: return jsonify(status=409, message=e.message) # Otherwise... else: # Try to find an existing auth and user. auth = Auth.for_provider(provider, provider_id) # If one is found, update the auth. if auth: user = auth.user auth.update_token(token, secret) # Otherwise create a new user and auth. else: user = User(**userdata) auth = Auth(provider, provider_id, token, secret) auth.user = user db.session.add(user) db.session.add(auth) db.session.commit() login_user(user)
def test_get_single_user(self): user = User(**self.userdata) self.db.session.add(user) self.db.session.commit() r = self.client.get('/users/1') self.assertEqual(self.json(r)['name'], self.userdata['name'])