def test_manage_memberships():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    membership = PubNub(config).manage_memberships()
    membership.include(['custom']).limit(30).end('XXX')

    with pytest.raises(PubNubException):
        membership.validate_params()

    membership.user_id('foo')
    assert membership.build_path(
    ) == ManageMemberships.MANAGE_MEMBERSHIPS_PATH % (SUB_KEY, 'foo')

    params = membership.custom_params()
    assert params['include'] == 'custom'
    assert params['limit'] == 30
    assert params['end'] == 'XXX'
    assert 'count' not in params

    membership.start('YYY').count(True)
    params = membership.custom_params()
    assert 'end' not in params
    assert params['start'] == 'YYY'
    assert params['count'] is True

    assert AUTH == membership.build_params_callback()({})['auth']
    membership.data({"add": [{"id": "my-channel"}]})
    assert membership.build_data() == '{"add": [{"id": "my-channel"}]}'
def test_update_space():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    space = PubNub(config).update_space()
    space.include('custom')
    with pytest.raises(PubNubException):
        space.build_path()

    space.space_id('foo')
    assert space.build_path() == UpdateSpace.UPDATE_SPACE_PATH % (SUB_KEY,
                                                                  'foo')
    with pytest.raises(PubNubException):
        space.validate_params()
    space.data({'name': 'bar'})
    assert json.loads(space.build_data()) == {'name': 'bar'}
    assert AUTH == space.build_params_callback()({})['auth']
def test_create_user():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    user = PubNub(config).create_user()
    with pytest.raises(PubNubException):
        user.validate_params()
    user.include({'name': 'a'})
    with pytest.raises(PubNubException):
        user.validate_params()
    user.include({'id': 'x'})
    with pytest.raises(PubNubException):
        user.validate_params()
    user.include('id')
    with pytest.raises(PubNubException):
        user.validate_params()
    user.data({'id': 'user', 'name': 'username'})
    user.validate_params()

    assert user.build_path() == CreateUser.CREATE_USER_PATH % SUB_KEY
    assert AUTH == user.build_params_callback()({})['auth']
    assert json.loads(user.build_data()) == {'id': 'user', 'name': 'username'}
def test_create_space():
    config = PNConfiguration()
    config.subscribe_key = SUB_KEY
    config.auth_key = AUTH
    space = PubNub(config).create_space()
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include({'name': 'a'})
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include({'id': 'x'})
    with pytest.raises(PubNubException):
        space.validate_params()
    space.include('custom')
    with pytest.raises(PubNubException):
        space.validate_params()
    space.data({'id': 'x', 'name': 'a'})
    space.validate_params()

    assert space.build_path() == CreateSpace.CREATE_SPACE_PATH % SUB_KEY
    assert AUTH == space.build_params_callback()({})['auth']
    assert json.loads(space.build_data()) == {'id': 'x', 'name': 'a'}