Пример #1
0
    def test_merge_users(self):
        user1_id = self.db.users.insert({
            'email': '*****@*****.**',
            'twitter_id': 1234,
        })
        self._add_authorized_app(user1_id, 'a')
        self._add_authorized_app(user1_id, 'b')
        self.db.passwords.insert({
            'owner': user1_id,
            'password': '******',
        })
        self.db.passwords.insert({
            'owner': user1_id,
            'password': '******',
        })
        user1 = self.db.users.find_one({'_id': user1_id})

        user2_id = self.db.users.insert({
            'email': '*****@*****.**',
            'google_id': 4321,
        })
        self._add_authorized_app(user2_id, 'b')
        self._add_authorized_app(user2_id, 'c')
        self.db.passwords.insert({
            'owner': user2_id,
            'password': '******',
        })
        self.db.passwords.insert({
            'owner': user2_id,
            'password': '******',
        })
        user2 = self.db.users.find_one({'_id': user2_id})

        merge_users(self.db, user1, user2)
        self.assertEqual(4, self.db.passwords.find(
            {'owner': user1_id}).count())
        self.assertEqual(0, self.db.passwords.find(
            {'owner': user2_id}).count())
        self.assertEqual(None, self.db.users.find_one({'_id': user2_id}))
        user1_refreshed = self.db.users.find_one({'_id': user1_id})
        self.assertEqual(user1_refreshed, {
            '_id': user1_id,
            'email': '*****@*****.**',
            'twitter_id': 1234,
            'google_id': 4321,
        })
        auths = self.db.authorized_apps.find({'user': user1_id})
        for real, expected in zip(auths, ['a', 'b', 'c']):
            self.assertEqual(real['client_id'], expected)

        auths = self.db.authorized_apps.find({'user': user2_id})
        self.assertEqual(auths.count(), 0)
    def test_merge_users(self):
        user1_id = self.db.users.insert({
                'email': '*****@*****.**',
                'twitter_id': 1234,
                'authorized_apps': ['a', 'b'],
                }, safe=True)
        self.db.passwords.insert({
                'owner': user1_id,
                'password': '******',
                }, safe=True)
        self.db.passwords.insert({
                'owner': user1_id,
                'password': '******',
                }, safe=True)
        user1 = self.db.users.find_one({'_id': user1_id}, safe=True)

        user2_id = self.db.users.insert({
                'email': '*****@*****.**',
                'google_id': 4321,
                'authorized_apps': ['b', 'c'],
                }, safe=True)
        self.db.passwords.insert({
                'owner': user2_id,
                'password': '******',
                }, safe=True)
        self.db.passwords.insert({
                'owner': user2_id,
                'password': '******',
                }, safe=True)
        user2 = self.db.users.find_one({'_id': user2_id}, safe=True)

        merge_users(self.db, user1, user2)
        self.assertEqual(4, self.db.passwords.find(
                {'owner': user1_id}, safe=True).count())
        self.assertEqual(0, self.db.passwords.find(
                {'owner': user2_id}, safe=True).count())
        self.assertEqual(None, self.db.users.find_one({'_id': user2_id}))
        user1_refreshed = self.db.users.find_one({'_id': user1_id}, safe=True)
        self.assertEqual(user1_refreshed, {
                '_id': user1_id,
                'email': '*****@*****.**',
                'twitter_id': 1234,
                'google_id': 4321,
                'authorized_apps': ['a', 'b', 'c'],
                })
Пример #3
0
    def test_merge_users(self):
        with transaction.manager:
            user1 = User(email='*****@*****.**')
            identity1 = ExternalIdentity(provider='twitter', external_id='1234',
                                         user=user1)
            password1 = Password(secret='s3cr3t1', user=user1)
            password2 = Password(secret='s3cr3t2', user=user1)

            user2 = User(email='*****@*****.**')
            identity2 = ExternalIdentity(provider='google', external_id='4321',
                                         user=user2)
            password3 = Password(secret='s3cr3t3', user=user2)
            password4 = Password(secret='s3cr3t4', user=user2)

            Session.add(user1)
            Session.add(identity1)
            Session.add(password1)
            Session.add(password2)
            Session.add(user2)
            Session.add(identity2)
            Session.add(password3)
            Session.add(password4)
            Session.flush()
            user1_id = user1.id
            user2_id = user2.id

        user1 = Session.query(User).filter(User.id == user1_id).one()
        user2 = Session.query(User).filter(User.id == user2_id).one()
        self.assertEqual(1, len(user1.identities))
        self.assertEqual(4, Session.query(Password).count())

        with transaction.manager:
            merge_users(user1, user2)

        self.assertEqual(4, Session.query(Password).count())
        self.assertEqual(0, Session.query(Password).filter(
            Password.user_id == user2_id).count())
        try:
            user2_refreshed = Session.query(User).filter(User.id == user2_id).one()
        except NoResultFound:
            user2_refreshed = None
        self.assertEqual(user2_refreshed, None)

        user1 = Session.query(User).filter(User.id == user1_id).one()
        self.assertEqual(2, len(user1.identities))