Esempio n. 1
0
 def get_client_for(self, username):
     """
     Creates an autenticated client
     """
     api = OpenDiscussionsApi(
         settings.OPEN_DISCUSSIONS_JWT_SECRET,
         settings.OPEN_DISCUSSIONS_BASE_URL,
         username,
     )
     return api._get_authenticated_session()
Esempio n. 2
0
def record(name, username, roles=None):
    """
    Record a cassette of some reddit communication.

    Usage:
        with record('cassette_name', 'username', roles=[ROLE_STAFF]) as api:
            api.users.list()

    Args:
        name (str): The name of the new cassette
        username (str): username to authenticate with
        roles (list(str)): roles the user has
    """
    setup_betamax()
    session = requests.Session()
    session.verify = False

    with patch('open_discussions_api.client.OpenDiscussionsApi._get_session', return_value=session):
        with Betamax(session).use_cassette(name):
            api = OpenDiscussionsApi(
                'terribly_unsafe_default_jwt_secret_key',
                'http://localhost:8063/',
                username,
                roles=roles or []
            )
            yield api
Esempio n. 3
0
    def on_start(self):
        """on_start is called before any task is scheduled """
        self.discussion_usernames = []
        self.discussion_channels = []

        # monkey patch the library client
        OpenDiscussionsApi._get_session = utils.patch_get_session(self.client)
        UsersApi.update = utils.patched_user_update
        ChannelsApi.add_contributor = utils.patched_add_contributor
        ChannelsApi.remove_contributor = utils.patched_remove_contributor
        ChannelsApi.add_moderator = utils.patched_add_moderator
        ChannelsApi.remove_moderator = utils.patched_remove_moderator
        ChannelsApi.add_subscriber = utils.patched_add_subscriber
        ChannelsApi.remove_subscriber = utils.patched_remove_subscriber

        self.api = OpenDiscussionsApi(
            settings.OPEN_DISCUSSIONS_JWT_SECRET,
            settings.OPEN_DISCUSSIONS_BASE_URL,
            settings.OPEN_DISCUSSIONS_API_USERNAME,
            roles=['staff']
        )

        # create a some users to play with
        for _ in range(2):
            self.create_user()
        # create a couple of channels
        for _ in range(1):
            self.create_channel()
    def on_start(self):
        """on_start is called before any task is scheduled """
        self.usernames = []
        self.channels = []
        self.posts = []

        # monkey patch the library client
        OpenDiscussionsApi._get_session = utils.patch_get_session(self.client)
        UsersApi.update = utils.patched_user_update
        ChannelsApi.add_contributor = utils.patched_add_contributor
        ChannelsApi.remove_contributor = utils.patched_remove_contributor
        ChannelsApi.add_moderator = utils.patched_add_moderator
        ChannelsApi.remove_moderator = utils.patched_remove_moderator
        ChannelsApi.add_subscriber = utils.patched_add_subscriber
        ChannelsApi.remove_subscriber = utils.patched_remove_subscriber

        self.api = OpenDiscussionsApi(
            settings.OPEN_DISCUSSIONS_JWT_SECRET,
            settings.OPEN_DISCUSSIONS_BASE_URL,
            settings.OPEN_DISCUSSIONS_API_USERNAME,
            roles=['staff']
        )

        # create a some users to play with
        self.create_users()
        # create some channels
        self.create_channels()
        # add contributors
        self.add_contributors()
        self.add_posts()
        self.add_comments()
        time.sleep(settings.WAIT_BEFORE_LOAD_TEST_SECONDS)
def test_users():
    """Test that users api gets configured correctly"""
    client = OpenDiscussionsApi('secret',
                                'http://example.com',
                                'username',
                                version='v1')
    assert isinstance(client.users, UsersApi)
    assert client.users.session.headers['Authorization'].startswith('Bearer ')
    assert client.users.base_url == 'http://example.com'
    assert client.users.version == 'v1'
    def on_start(self):
        """on_start is called before any task is scheduled """
        self.discussion_usernames = []

        # monkey patch the library client
        OpenDiscussionsApi._get_session = patch_get_session(self.client)
        UsersApi.update = patched_user_update

        self.api = OpenDiscussionsApi(
            settings.OPEN_DISCUSSIONS_JWT_SECRET,
            settings.OPEN_DISCUSSIONS_BASE_URL,
            settings.OPEN_DISCUSSIONS_API_USERNAME,
            roles=['staff']
        )
Esempio n. 7
0
def get_staff_client():
    """
    Gets a client configured for user management
    """
    if not settings.OPEN_DISCUSSIONS_JWT_SECRET:
        raise ImproperlyConfigured('OPEN_DISCUSSIONS_JWT_SECRET must be set')
    if not settings.OPEN_DISCUSSIONS_BASE_URL:
        raise ImproperlyConfigured('OPEN_DISCUSSIONS_BASE_URL must be set')
    if not settings.OPEN_DISCUSSIONS_API_USERNAME:
        raise ImproperlyConfigured('OPEN_DISCUSSIONS_API_USERNAME must be set')

    return OpenDiscussionsApi(settings.OPEN_DISCUSSIONS_JWT_SECRET,
                              settings.OPEN_DISCUSSIONS_BASE_URL,
                              settings.OPEN_DISCUSSIONS_API_USERNAME,
                              roles=[ROLE_STAFF])
Esempio n. 8
0
def api_client(use_betamax):
    """API client"""
    return OpenDiscussionsApi('secret',
                              'http://localhost:8063/',
                              'mitodl',
                              roles=[ROLE_STAFF])
def test_client_invalid_secret():
    """Test that the API errors if not passed secret"""
    with pytest.raises(AttributeError) as err:
        OpenDiscussionsApi('', 'http://example.com', 'username')
    assert str(err.value) == "secret is required"
def test_client_invalid_base_url():
    """Test that the API errors if not passed base_url"""
    with pytest.raises(AttributeError) as err:
        OpenDiscussionsApi('secret', '', 'username')
    assert str(err.value) == "base_url is required"