def loading_cache_checker(read_data, expected):
    open_ = mock_open(read_data=read_data + '\n')
    open_().readlines.return_value = read_data.split('\n')
    open_().readline.side_effect = read_data.split('\n')
    with patch.object(builtins, 'open', open_, create=True):
        oauth2_instanced = oauth2.CourseraOAuth2('id', 'secret', 'scopes')
        token_cache = oauth2_instanced.token_cache
        assert token_cache == expected, 'Token cache was: %s' % token_cache
def check_cache_types_impl(cache_type, should_be_allowed):
    oauth2_client = oauth2.CourseraOAuth2('id', 'secret', 'fake scopes')
    result = oauth2_client._check_token_cache_type(cache_type)
    assert result == should_be_allowed, \
        'Got %(result)s. Expected: %(expected)s' % {
            'result': result,
            'expected': should_be_allowed,
        }
def test_build_authorization_url():
    oauth2_client = oauth2.CourseraOAuth2('my_fake_client_id',
                                          'my_fake_secret',
                                          'view_profile manage_graders')

    state_token = 'my_fake_state_token'

    actual = oauth2_client._build_authorizaton_url(state_token)

    expected_url = ('https://accounts.coursera.org/oauth2/v1/auth?'
                    'access_type=offline&'
                    'state=my_fake_state_token&'
                    'redirect_uri=http%3A%2F%2Flocalhost%3A9876%2Fcallback&'
                    'response_type=code&'
                    'client_id=my_fake_client_id&'
                    'scope=view_profile+manage_graders')

    assert expected_url == actual, 'Got unexpected URL: %s' % actual