Esempio n. 1
0
def lambda_handler(event, context):
    login_twitter_index = LoginTwitterIndex(
        event=event,
        context=context,
        dynamodb=dynamodb,
        cognito=cognito
    )
    return login_twitter_index.main()
 def test_main_ok_with_creating_new_user(self):
     with patch('login_twitter_index.TwitterUtil') as twitter_mock, \
      patch('login_twitter_index.UserUtil') as user_mock:
         twitter_mock.return_value.get_user_info.return_value = {
             'user_id': 'Twitter-1234',
             'email': '*****@*****.**',
             'display_name': 'my_name'
         }
         user_mock.exists_user.return_value = False
         user_mock.create_external_provider_user.return_value = {
             'AuthenticationResult': {
                 'AccessToken': 'aaaaa',
                 'IdToken': 'bbbbb',
                 'RefreshToken': 'ccccc'
             }
         }
         user_mock.add_external_provider_user_info.return_value = None
         params = {
             'body': {
                 'oauth_token': 'fake_oauth_token',
                 'oauth_verifier': 'fake_oauth_verifier'
             }
         }
         params['body'] = json.dumps(params['body'])
         response = LoginTwitterIndex(params, {}).main()
         self.assertEqual(response['statusCode'], 200)
         self.assertEqual(
             json.loads(response['body']), {
                 'access_token': 'aaaaa',
                 'id_token': 'bbbbb',
                 'refresh_token': 'ccccc',
                 'last_auth_user': '******',
                 'has_user_id': False,
                 'status': 'sign_up'
             })
 def test_main_ng_with_awsexception_and_new_user(self):
     with patch('login_twitter_index.TwitterUtil') as twitter_mock, \
      patch('login_twitter_index.UserUtil') as user_mock:
         twitter_mock.return_value.get_user_info.return_value = {
             'user_id': 'Twitter-1234',
             'email': '*****@*****.**',
             'display_name': 'my_name'
         }
         user_mock.exists_user.return_value = False
         user_mock.create_external_provider_user.return_value = ClientError(
             {'Error': {
                 'Code': 'xxxxxx'
             }}, 'operation_name')
         user_mock.force_non_verified_phone.return_value = None
         user_mock.add_user_profile.return_value = None
         user_mock.add_external_provider_user_info.return_value = None
         user_mock.has_user_id.return_value = True
         params = {
             'body': {
                 'oauth_token': 'fake_oauth_token',
                 'oauth_verifier': 'fake_oauth_verifier'
             }
         }
         params['body'] = json.dumps(params['body'])
         response = LoginTwitterIndex(params, {}).main()
         self.assertEqual(response['statusCode'], 500)
         self.assertEqual(
             json.loads(response['body']),
             {'message': 'Internal server error: LoginTwitterIndex'})
 def test_main_ng_with_twitterexception(self):
     with patch('login_twitter_index.TwitterUtil') as twitter_mock:
         twitter_mock.return_value.get_user_info.side_effect = TwitterOauthError(
             endpoint='http://example.com',
             status_code=500,
             message='error')
         params = {
             'body': {
                 'oauth_token': 'fake_oauth_token',
                 'oauth_verifier': 'fake_oauth_verifier'
             }
         }
         params['body'] = json.dumps(params['body'])
         response = LoginTwitterIndex(params, {}).main()
         self.assertEqual(response['statusCode'], 500)
         self.assertEqual(json.loads(response['body']),
                          {'message': 'Internal server error'})
 def test_main_ok_with_existing_user_and_user_id(self):
     with patch('login_twitter_index.TwitterUtil') as twitter_mock, \
      patch('login_twitter_index.UserUtil') as user_mock, \
      patch('login_twitter_index.CryptoUtil') as crypto_mock:
         twitter_mock.return_value.get_user_info.return_value = {
             'user_id': 'Twitter-12345',
             'email': '*****@*****.**',
             'display_name': 'my_name'
         }
         user_mock.exists_user.return_value = True
         user_mock.has_user_id.return_value = True
         crypto_mock.decrypt_password.return_value = 'password'
         user_mock.get_user_id.return_value = 'user_id'
         user_mock.external_provider_login.return_value = {
             'AuthenticationResult': {
                 'AccessToken': 'aaaaa',
                 'IdToken': 'bbbbb',
                 'RefreshToken': 'ccccc'
             }
         }
         params = {
             'body': {
                 'oauth_token': 'fake_oauth_token',
                 'oauth_verifier': 'fake_oauth_verifier'
             }
         }
         params['body'] = json.dumps(params['body'])
         response = LoginTwitterIndex(params, {}, dynamodb=dynamodb).main()
         self.assertEqual(response['statusCode'], 200)
         self.assertEqual(
             json.loads(response['body']), {
                 'access_token': 'aaaaa',
                 'id_token': 'bbbbb',
                 'refresh_token': 'ccccc',
                 'last_auth_user': '******',
                 'has_user_id': True,
                 'status': 'login'
             })
         user_mock.external_provider_login.assert_called_with(
             cognito=None,
             password='******',
             provider='xxxxx',
             user_id='user_id',
             user_pool_app_id='user_pool_id',
             user_pool_id='user_pool_id')
 def assert_bad_request(self, params):
     response = LoginTwitterIndex(params, {}).main()
     self.assertEqual(response['statusCode'], 400)