Пример #1
0
 def test_get_accounts_multiple_providers(self):
     user = User(email='*****@*****.**', email_verified=True)
     identity1 = ExternalIdentity(user=user,
                                  provider='twitter',
                                  external_id='1234')
     identity2 = ExternalIdentity(user=user,
                                  provider='google',
                                  external_id='4321')
     password = Password(user=user, secret='secret')
     Session.add(user)
     Session.add(identity1)
     Session.add(identity2)
     Session.add(password)
     Session.flush()
     self.assertEqual(user.get_accounts('google'), [{
         'id':
         user.id,
         'is_current':
         True,
         'is_verified':
         True,
         'passwords':
         1,
         'providers': [{
             'name': 'twitter',
             'is_current': False,
         }, {
             'name': 'google',
             'is_current': True,
         }]
     }])
Пример #2
0
def create_user(email='',
                email_verified=False,
                provider='twitter',
                external_id='twitter1',
                **kwargs):
    date = datetime.datetime(2012, 12, 12, 12, 12)
    user = User(screen_name='John Doe',
                first_name='John',
                last_name='Doe',
                email=email,
                email_verified=email_verified,
                creation=date,
                last_login=date,
                **kwargs)
    identity = ExternalIdentity(provider=provider,
                                external_id=external_id,
                                user=user)

    with transaction.manager:
        Session.add(user)
        Session.add(identity)
        Session.flush()
        user_id = user.id

    return user_id
Пример #3
0
    def test_register_or_update_existing_user(self):
        user = User(screen_name='JohnDoe',
                    first_name='John',
                    last_name='')
        identity = ExternalIdentity(provider='twitter',
                                    external_id='1',
                                    user=user)
        Session.add(user)
        Session.add(identity)
        Session.flush()
        user_id = user.id

        request = testing.DummyRequest()
        request.session = {USER_ATTR: True}
        request.google_analytics = GoogleAnalytics(request)
        response = register_or_update(request, 'twitter', '1', {
            'screen_name': 'JohnDoe',
            'first_name': 'John',
            'last_name': 'Doe',
            'email': '*****@*****.**',
        }, '/next')
        self.assertEqual(response.status, '302 Found')
        self.assertEqual(response.location, '/next')
        user = Session.query(User).filter(User.id == user_id).one()
        self.assertEqual(user.email, '*****@*****.**')
        self.assertEqual(user.last_name, 'Doe')
        self.assertEqual(user.allow_google_analytics, True)
Пример #4
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))
Пример #5
0
    def test_twitter_callback_existing_user(self, post_mock, get_mock):
        # good request, twitter is happy now. Existing user
        user = User(screen_name='Johnny')
        identity = ExternalIdentity(user=user,
                                    provider='twitter',
                                    external_id='user1')

        with transaction.manager:
            Session.add(user)
            Session.add(identity)
            Session.flush()
            user_id = user.id

        mock0 = mock.Mock()
        mock0.status_code = 200
        mock0.text = 'oauth_callback_confirmed=true&oauth_token=123456789'

        mock1 = mock.Mock()
        mock1.status_code = 200
        mock1.text = 'oauth_token=xyz&user_id=user1&screen_name=JohnDoe'

        mock2 = mock.Mock()
        mock2.ok = True
        mock2.json = lambda: {
            'token_type': 'bearer',
            'access_token': '1234567890',
        }

        post_mock.side_effect = [mock0, mock1, mock2]

        get_response = get_mock.return_value
        get_response.ok = True
        get_response.json = lambda: {
            'name': 'John Doe',
        }

        self.testapp.get('/twitter/login')

        good_url = '/twitter/callback?oauth_token=123456789&oauth_verifier=abc'
        res = self.testapp.get(good_url, status=302)
        self.assertEqual(res.status, '302 Found')
        self.assertEqual(res.location, 'http://localhost/')
        self.assertTrue('Set-Cookie' in res.headers)

        # even if the response from twitter included a different
        # screen_name, our user will not be updated
        new_user = Session.query(User).filter(User.id == user_id).one()
        self.assertEqual(new_user.screen_name, 'Johnny')
Пример #6
0
    def test_twitter_callback_existing_user_remember_url(
            self, post_mock, get_mock):
        # good request, existing user, remember next_url
        user = User(screen_name='Johnny')
        identity = ExternalIdentity(user=user,
                                    provider='twitter',
                                    external_id='user1')

        with transaction.manager:
            Session.add(user)
            Session.add(identity)

        mock0 = mock.Mock()
        mock0.status_code = 200
        mock0.text = 'oauth_callback_confirmed=true&oauth_token=123456789'

        mock1 = mock.Mock()
        mock1.status_code = 200
        mock1.text = 'oauth_token=xyz&user_id=user1&screen_name=JohnDoe'

        mock2 = mock.Mock()
        mock2.ok = True
        mock2.json = lambda: {
            'token_type': 'bearer',
            'access_token': '1234567890',
        }

        post_mock.side_effect = [mock0, mock1, mock2]

        get_response = get_mock.return_value
        get_response.ok = True
        get_response.json = lambda: {
            'name': 'John Doe',
        }

        self.testapp.get('/twitter/login?next_url=http://localhost/foo/bar')

        good_url = '/twitter/callback?oauth_token=123456789&oauth_verifier=abc'
        res = self.testapp.get(good_url, status=302)
        self.assertEqual(res.status, '302 Found')
        self.assertEqual(res.location, 'http://localhost/foo/bar')
        self.assertTrue('Set-Cookie' in res.headers)
Пример #7
0
 def test_get_accounts_one_provider_email_verified(self):
     user = User(email='*****@*****.**', email_verified=True)
     identity = ExternalIdentity(user=user,
                                 provider='twitter',
                                 external_id='1234')
     Session.add(user)
     Session.add(identity)
     Session.flush()
     self.assertEqual(
         user.get_accounts(''),
         [{
             'id': user.id,
             'is_current': False,
             'is_verified': True,
             'passwords': 0,
             'providers': [{
                 'name': 'twitter',
                 'is_current': False,
             }]
         }])
Пример #8
0
    def test_register_or_update_next_url_in_session(self):
        user = User(screen_name='JohnDoe',
                    first_name='John',
                    last_name='')
        identity = ExternalIdentity(provider='twitter',
                                    external_id='1',
                                    user=user)
        Session.add(user)
        Session.add(identity)
        Session.flush()

        request = testing.DummyRequest()
        request.session = {'next_url': '/foo'}
        request.google_analytics = GoogleAnalytics(request)
        response = register_or_update(request, 'twitter', '1', {
            'screen_name': 'JohnDoe',
            'first_name': 'John',
            'last_name': 'Doe',
            'email': '*****@*****.**',
        }, '/next')
        self.assertEqual(response.status, '302 Found')
        self.assertEqual(response.location, '/foo')
Пример #9
0
    def test_non_empty_database(self):
        d = datetime.datetime
        with transaction.manager:
            user1 = User(first_name='John',
                         last_name='Doe',
                         creation=d(2012, 12, 12, 12, 12, 12),
                         last_login=d(2012, 12, 12, 12, 12, 12),
                         email='*****@*****.**')
            Session.add(user1)
            user2 = User(first_name='John2',
                         last_name='Doe2',
                         creation=d(2012, 12, 12, 12, 12, 12),
                         last_login=d(2012, 12, 12, 12, 12, 12),
                         email='*****@*****.**',
                         email_verified=True)
            identity2 = ExternalIdentity(user=user2, provider='twitter',
                                         external_id='1234')
            Session.add(user2)
            Session.add(identity2)
            self.add_passwords(user2, 1)
            user3 = User(first_name='John3',
                         last_name='Doe3',
                         creation=d(2012, 12, 12, 12, 12, 12),
                         last_login=d(2012, 12, 12, 12, 12, 12),
                         email='*****@*****.**',
                         email_verified=True)
            identity3_1 = ExternalIdentity(user=user3, provider='twitter',
                                           external_id='1234')
            identity3_2 = ExternalIdentity(user=user3, provider='facebook',
                                           external_id='5678')
            identity3_3 = ExternalIdentity(user=user3, provider='google',
                                           external_id='abcd')
            Session.add(user3)
            Session.add(identity3_1)
            Session.add(identity3_2)
            Session.add(identity3_3)
            self.add_passwords(user3, 2)

            Session.flush()
            u1_id = user1.id
            u2_id = user2.id
            u3_id = user3.id
        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = users()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()
        context = {
            'u1': u1_id,
            'u2': u2_id,
            'u3': u3_id,
            'tab': '\t',
        }
        expected_output = """John Doe <*****@*****.**> (%(u1)s)
%(tab)sPasswords: 0
%(tab)sProviders:
%(tab)sVerified: False
%(tab)sDate joined: 2012-12-12 12:12:12
%(tab)sLast login: 2012-12-12 12:12:12

John2 Doe2 <*****@*****.**> (%(u2)s)
%(tab)sPasswords: 1
%(tab)sProviders: twitter
%(tab)sVerified: True
%(tab)sDate joined: 2012-12-12 12:12:12
%(tab)sLast login: 2012-12-12 12:12:12

John3 Doe3 <*****@*****.**> (%(u3)s)
%(tab)sPasswords: 2
%(tab)sProviders: facebook, google, twitter
%(tab)sVerified: True
%(tab)sDate joined: 2012-12-12 12:12:12
%(tab)sLast login: 2012-12-12 12:12:12

""" % context
        self.assertEqual(stdout, expected_output)
Пример #10
0
    def test_statistics_non_empty_database(self):
        with transaction.manager:
            user1 = User(first_name='John',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=True)
            identity1 = ExternalIdentity(user=user1, provider='google',
                                         external_id='1')
            Session.add(user1)
            Session.add(identity1)
            self.add_passwords(user1, 10)

            user2 = User(first_name='Peter',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=False)
            identity2 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='1')
            Session.add(user2)
            Session.add(identity2)
            self.add_passwords(user2, 20)

            user3 = User(first_name='Susan',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=False)
            identity3 = ExternalIdentity(user=user1, provider='facebook',
                                         external_id='1')
            Session.add(user3)
            Session.add(identity3)
            self.add_passwords(user3, 15)

            user4 = User(first_name='Alice',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity4 = ExternalIdentity(user=user1, provider='persona',
                                         external_id='1')
            Session.add(user4)
            Session.add(identity4)

            user5 = User(first_name='Bob',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity5 = ExternalIdentity(user=user1, provider='google',
                                         external_id='2')
            Session.add(user5)
            Session.add(identity5)

            user6 = User(first_name='Kevin',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity6 = ExternalIdentity(user=user1, provider='google',
                                         external_id='3')
            Session.add(user6)
            Session.add(identity6)

            user7 = User(first_name='Maria',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity7 = ExternalIdentity(user=user1, provider='google',
                                         external_id='4')
            Session.add(user7)
            Session.add(identity7)

            user8 = User(first_name='Bran',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity8 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='2')
            Session.add(user8)
            Session.add(identity8)

            user9 = User(first_name='George',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity9 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='3')
            Session.add(user9)
            Session.add(identity9)

            user10 = User(first_name='Travis',
                          last_name='Doe',
                          email='',
                          email_verified=False,
                          allow_google_analytics=False)
            identity10 = ExternalIdentity(user=user1, provider='persona',
                                          external_id='2')
            Session.add(user10)
            Session.add(identity10)
            Session.flush()

        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = statistics()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()

        expected_output = """Number of users: 10
Number of passwords: 45
Verified users: 30.00%% (3)
Users that allow Google Analytics cookie: 10.00%% (1)
Identity providers:
%(tab)sgoogle: 40.00%% (4)
%(tab)stwitter: 30.00%% (3)
%(tab)spersona: 20.00%% (2)
%(tab)sfacebook: 10.00%% (1)
Email providers:
%(tab)sexample.com: 66.67%% (2)
%(tab)sOthers: 33.33%% (1)
Users without email: 70.00%% (7)
Most active users:
%(tab)sPeter Doe <*****@*****.**>: 20
%(tab)sSusan Doe <*****@*****.**>: 15
%(tab)sJohn Doe <*****@*****.**>: 10
Users without passwords: 70.00%% (7)
""" % {'tab': '\t'}
        self.assertEqual(stdout, expected_output)