moto/tests/test_cognitoidentity/test_cognitoidentity.py

142 lines
5.1 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import boto3
2019-09-01 15:38:33 +00:00
from botocore.exceptions import ClientError
from nose.tools import assert_raises
2018-04-03 21:08:20 +00:00
from moto import mock_cognitoidentity
from moto.cognitoidentity.utils import get_random_identity_id
@mock_cognitoidentity
def test_create_identity_pool():
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.create_identity_pool(IdentityPoolName='TestPool',
2018-04-03 21:08:20 +00:00
AllowUnauthenticatedIdentities=False,
SupportedLoginProviders={'graph.facebook.com': '123456789012345'},
DeveloperProviderName='devname',
2018-04-03 21:08:20 +00:00
OpenIdConnectProviderARNs=['arn:aws:rds:eu-west-2:123456789012:db:mysql-db'],
CognitoIdentityProviders=[
{
'ProviderName': 'testprovider',
'ClientId': 'CLIENT12345',
'ServerSideTokenCheck': True
},
],
2018-04-03 21:08:20 +00:00
SamlProviderARNs=['arn:aws:rds:eu-west-2:123456789012:db:mysql-db'])
assert result['IdentityPoolId'] != ''
2018-04-03 21:08:20 +00:00
2019-09-01 15:38:33 +00:00
@mock_cognitoidentity
def test_describe_identity_pool():
conn = boto3.client('cognito-identity', 'us-west-2')
res = conn.create_identity_pool(IdentityPoolName='TestPool',
AllowUnauthenticatedIdentities=False,
SupportedLoginProviders={'graph.facebook.com': '123456789012345'},
DeveloperProviderName='devname',
OpenIdConnectProviderARNs=['arn:aws:rds:eu-west-2:123456789012:db:mysql-db'],
CognitoIdentityProviders=[
{
'ProviderName': 'testprovider',
'ClientId': 'CLIENT12345',
'ServerSideTokenCheck': True
},
],
SamlProviderARNs=['arn:aws:rds:eu-west-2:123456789012:db:mysql-db'])
result = conn.describe_identity_pool(IdentityPoolId=res['IdentityPoolId'])
assert result['IdentityPoolId'] == res['IdentityPoolId']
assert result['AllowUnauthenticatedIdentities'] == res['AllowUnauthenticatedIdentities']
assert result['SupportedLoginProviders'] == res['SupportedLoginProviders']
assert result['DeveloperProviderName'] == res['DeveloperProviderName']
assert result['OpenIdConnectProviderARNs'] == res['OpenIdConnectProviderARNs']
assert result['CognitoIdentityProviders'] == res['CognitoIdentityProviders']
assert result['SamlProviderARNs'] == res['SamlProviderARNs']
@mock_cognitoidentity
def test_describe_identity_pool_with_invalid_id_raises_error():
conn = boto3.client('cognito-identity', 'us-west-2')
with assert_raises(ClientError) as cm:
conn.describe_identity_pool(IdentityPoolId='us-west-2_non-existent')
cm.exception.operation_name.should.equal('DescribeIdentityPool')
cm.exception.response['Error']['Code'].should.equal('ResourceNotFoundException')
cm.exception.response['ResponseMetadata']['HTTPStatusCode'].should.equal(400)
# testing a helper function
def test_get_random_identity_id():
assert len(get_random_identity_id('us-west-2')) > 0
2018-10-09 17:28:15 +00:00
assert len(get_random_identity_id('us-west-2').split(':')[1]) == 19
@mock_cognitoidentity
def test_get_id():
# These two do NOT work in server mode. They just don't return the data from the model.
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.get_id(AccountId='someaccount',
IdentityPoolId='us-west-2:12345',
Logins={
2018-04-03 21:08:20 +00:00
'someurl': '12345'
})
2018-04-03 21:08:20 +00:00
print(result)
2019-09-01 15:38:33 +00:00
assert result.get('IdentityId', "").startswith('us-west-2') or result.get('ResponseMetadata').get(
'HTTPStatusCode') == 200
2018-04-03 21:08:20 +00:00
@mock_cognitoidentity
def test_get_credentials_for_identity():
2018-04-04 07:28:39 +00:00
# These two do NOT work in server mode. They just don't return the data from the model.
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.get_credentials_for_identity(IdentityId='12345')
2018-04-04 00:22:21 +00:00
assert result.get('Expiration', 0) > 0 or result.get('ResponseMetadata').get('HTTPStatusCode') == 200
assert result.get('IdentityId') == '12345' or result.get('ResponseMetadata').get('HTTPStatusCode') == 200
2018-04-03 21:08:20 +00:00
@mock_cognitoidentity
def test_get_open_id_token_for_developer_identity():
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.get_open_id_token_for_developer_identity(
IdentityPoolId='us-west-2:12345',
IdentityId='12345',
Logins={
'someurl': '12345'
},
TokenDuration=123
)
assert len(result['Token']) > 0
assert result['IdentityId'] == '12345'
2018-10-09 17:28:15 +00:00
2019-09-01 15:38:33 +00:00
2018-10-09 17:28:15 +00:00
@mock_cognitoidentity
def test_get_open_id_token_for_developer_identity_when_no_explicit_identity_id():
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.get_open_id_token_for_developer_identity(
IdentityPoolId='us-west-2:12345',
Logins={
'someurl': '12345'
},
TokenDuration=123
)
assert len(result['Token']) > 0
assert len(result['IdentityId']) > 0
2019-09-01 15:38:33 +00:00
@mock_cognitoidentity
def test_get_open_id_token():
conn = boto3.client('cognito-identity', 'us-west-2')
result = conn.get_open_id_token(
IdentityId='12345',
Logins={
'someurl': '12345'
}
)
assert len(result['Token']) > 0
assert result['IdentityId'] == '12345'