def test_post(self, tenant, foobaz):
        assert_that(
            foobaz,
            has_entries(
                uuid=uuid_(),
                name='foobaz',
                slug='foobaz',
                description=none(),
                acl=empty(),
                shared=False,
                tenant_uuid=self.top_tenant_uuid,
            ),
        )

        policy_args = {
            'name': 'foobar',
            'slug': 'slug1',
            'description': 'a test policy',
            'acl': ['dird.me.#', 'ctid-ng.#'],
            'shared': False,
            'tenant_uuid': tenant['uuid'],
        }
        # Specify the tenant_uuid
        with self.policy(self.client, **policy_args) as policy:
            assert_that(policy, has_entries(uuid=uuid_(), name='foobar'))

        # Specify the tenant uuid in another sub-tenant tree
        with self.client_in_subtenant() as (client, _, __):
            assert_http_error(401, client.policies.new, **policy_args)

        # Invalid body
        assert_http_error(400, self.client.policies.new, '')
Beispiel #2
0
    def test_email_updates_as_admin(self, foobar):
        assert_http_error(404, self.client.admin.update_user_emails,
                          UNKNOWN_UUID, [])
        assert_http_error(400, self.client.users.update_emails, foobar['uuid'],
                          [ONE, ONE])

        result = self.client.admin.update_user_emails(foobar['uuid'],
                                                      [ONE, TWO])
        assert_that(
            result,
            contains_inanyorder(has_entries(uuid=uuid_(), **ONE),
                                has_entries(uuid=uuid_(), **TWO)),
        )

        one_uuid = [
            entry['uuid'] for entry in result
            if entry['address'] == '*****@*****.**'
        ][0]
        result = self.client.admin.update_user_emails(foobar['uuid'],
                                                      [ONE, THREE])
        assert_that(
            result,
            contains_inanyorder(has_entries(uuid=one_uuid, **ONE),
                                has_entries(uuid=uuid_(), **THREE)),
        )
    def test_multi_tenant(self):
        main_tenant_client = self.get_client(VALID_TOKEN_MAIN_TENANT)
        sub_tenant_client = self.get_client(VALID_TOKEN_SUB_TENANT)

        with self.source(main_tenant_client, self.valid_body) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=MAIN_TENANT))

        with self.source(main_tenant_client,
                         self.valid_body,
                         tenant_uuid=SUB_TENANT) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=SUB_TENANT))

        with self.source(sub_tenant_client, self.valid_body) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=SUB_TENANT))

        assert_that(
            calling(sub_tenant_client.backends.create_source).with_args(
                'google', self.valid_body, tenant_uuid=MAIN_TENANT),
            raises(Exception).matching(HTTP_401),
        )

        with self.source(main_tenant_client, self.valid_body):
            assert_that(
                calling(sub_tenant_client.backends.create_source).with_args(
                    'google', self.valid_body),
                not_(raises(Exception)),
            )
    def test_multi_tenant(self):
        main_tenant_client = self.get_client(VALID_TOKEN_MAIN_TENANT)
        sub_tenant_client = self.get_client(VALID_TOKEN_SUB_TENANT)

        with self.source(main_tenant_client, self.valid_body) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=MAIN_TENANT))

        with self.source(main_tenant_client,
                         self.valid_body,
                         tenant_uuid=SUB_TENANT) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=SUB_TENANT))

        with self.source(sub_tenant_client, self.valid_body) as result:
            assert_that(result,
                        has_entries(uuid=uuid_(), tenant_uuid=SUB_TENANT))

        assert_that(
            calling(sub_tenant_client.phonebook_source.create).with_args(
                self.valid_body, tenant_uuid=MAIN_TENANT),
            raises(Exception).matching(
                has_properties(response=has_properties(status_code=401))),
        )

        with self.source(main_tenant_client, self.valid_body):
            assert_that(
                calling(sub_tenant_client.phonebook_source.create).with_args(
                    self.valid_body),
                not_(raises(Exception)),
            )
Beispiel #5
0
    def test_token_creation(self, user):
        response = self._post_token(user['username'],
                                    's3cr37',
                                    backend='wazo_user')
        assert_that(
            response,
            has_entries(
                token=uuid_(),
                auth_id=user['uuid'],
                xivo_user_uuid=user['uuid'],  # For API compatibility reason
                acl=has_item('default.user.policy'),
                session_uuid=uuid_(),
                metadata=has_entries(pbx_user_uuid=user['uuid']),
            ),
        )

        assert_http_error(
            401,
            self._post_token,
            user['username'],
            'not-our-password',
            backend='wazo_user',
        )
        assert_http_error(401,
                          self._post_token,
                          'not-foobar',
                          's3cr37',
                          backend='wazo_user')
Beispiel #6
0
    def test_register_post(self):
        args = {
            'username': '******',
            'lastname': 'Denver',
            'email_address': '*****@*****.**',
            'password': '******',
        }

        with self.user(self.client, register=True, **args) as user:
            assert_that(
                user,
                has_entries(
                    uuid=uuid_(),
                    username='******',
                    firstname=None,
                    lastname='Denver',
                    enabled=True,
                    tenant_uuid=uuid_(),
                    emails=contains_inanyorder(
                        has_entries(
                            uuid=uuid_(),
                            address='*****@*****.**',
                            main=True,
                            confirmed=False,
                        )),
                ),
            )

            created_tenant = self.client.tenants.get(user['tenant_uuid'])
            assert_that(
                created_tenant,
                has_entries(uuid=is_not(self.top_tenant_uuid),
                            parent_uuid=self.top_tenant_uuid),
            )

            url = self.get_last_email_url()
            url = url.replace('https', 'http')
            requests.get(url)

            updated_user = self.client.users.get(user['uuid'])
            assert_that(
                updated_user,
                has_entries(emails=contains_exactly(has_entries(
                    confirmed=True))),
            )

            tenants = self.client.users.get_tenants(user['uuid'])
            assert_that(
                tenants,
                has_entries(items=contains_exactly(has_entries(uuid=uuid_()))))
 def check():
     response = self.client.backends.list_sources(
         'google', tenant_uuid=self.tenant_uuid
     )
     assert_that(
         response,
         has_entries(
             items=has_item(
                 has_entries(
                     uuid=uuid_(),
                     tenant_uuid=self.tenant_uuid,
                     name='auto_google_mytenant',
                     auth={
                         'host': 'localhost',
                         'port': 9497,
                         'prefix': None,
                         'https': False,
                         'version': '0.1',
                     },
                     first_matched_columns=has_item('numbers'),
                     searched_columns=contains_inanyorder(
                         'name', 'numbers', 'familyName', 'givenName'
                     ),
                     format_columns=has_entries(
                         phone_mobile='{numbers_by_label[mobile]}',
                         reverse='{name}',
                         phone='{numbers_except_label[mobile][0]}',
                     ),
                 )
             )
         ),
     )
    def test_user_creation_email_confirmed(self):
        username = '******'
        hash_ = 'the_hashed_password'
        email_address = '*****@*****.**'

        user_uuid = self._user_dao.create(
            username=username,
            email_address=email_address,
            tenant_uuid=self.top_tenant_uuid,
            hash_=hash_,
            salt=self.salt,
            purpose='user',
            email_confirmed=True,
        )['uuid']

        try:
            result = self._user_dao.list_(uuid=user_uuid)
            assert_that(
                result,
                contains_exactly(
                    has_entries(
                        username=username,
                        emails=contains_exactly(
                            has_entries(uuid=uuid_(),
                                        address=email_address,
                                        confirmed=True)),
                    )),
            )
        finally:
            self._user_dao.delete(user_uuid)
    def test_get(self, group1, group2, user1, user2, policy):
        response = self.client.policies.get(policy['uuid'])
        assert_that(
            response,
            has_entries(
                name='foobar',
                description='a test policy',
                acl=contains_inanyorder('service1.me.#', 'service2.#'),
                read_only=False,
            ),
        )

        assert_http_error(404, self.client.policies.get, UNKNOWN_UUID)

        self.client.users.add_policy(user1['uuid'], policy['uuid'])
        self.client.users.add_policy(user2['uuid'], policy['uuid'])
        self.client.groups.add_policy(group1['uuid'], policy['uuid'])
        self.client.groups.add_policy(group2['uuid'], policy['uuid'])

        assert_that(
            self.client.policies.get(policy['uuid'])['acl'],
            contains_inanyorder('service1.me.#', 'service2.#'),
        )

        with self.client_in_subtenant() as (client, _, __):
            assert_http_error(404, client.policies.get, policy['uuid'])

            policy_in_subtenant = client.policies.new(name='in sub-tenant')
            assert_that(
                self.client.policies.get(policy_in_subtenant['uuid']),
                has_entries(uuid=uuid_(), name='in sub-tenant'),
            )
    def test_create_minimal_parameters(self):
        room_args = {'users': [{'uuid': UUID}]}

        room = self.chatd.rooms.create_from_user(room_args)

        assert_that(
            room,
            has_entries(
                uuid=uuid_(),
                name=none(),
                users=contains_inanyorder(
                    has_entries(
                        uuid=str(TOKEN_USER_UUID),
                        tenant_uuid=str(TOKEN_TENANT_UUID),
                        wazo_uuid=str(WAZO_UUID),
                    ),
                    has_entries(
                        uuid=room_args['users'][0]['uuid'],
                        tenant_uuid=str(TOKEN_TENANT_UUID),
                        wazo_uuid=str(WAZO_UUID),
                    ),
                ),
            ),
        )
        self._delete_room(room)
    def test_create(self):
        room_args = {
            'name':
            'test-room',
            'users': [
                {
                    'uuid': str(TOKEN_USER_UUID),
                    'tenant_uuid': str(TOKEN_TENANT_UUID),
                    'wazo_uuid': str(WAZO_UUID),
                },
                {
                    'uuid': UUID,
                    'tenant_uuid': UUID,
                    'wazo_uuid': UUID
                },
            ],
        }
        routing_key = 'chatd.users.*.rooms.created'
        event_accumulator = self.bus.accumulator(routing_key)

        room = self.chatd.rooms.create_from_user(room_args)

        assert_that(
            room,
            has_entries(
                uuid=uuid_(),
                name=room_args['name'],
                users=contains_inanyorder(*room_args['users']),
            ),
        )

        event = event_accumulator.accumulate(with_headers=True)
        assert_that(
            event,
            contains_inanyorder(
                has_entries(
                    message=has_entries(
                        data=has_entries(room_args),
                        required_acl=
                        f'events.chatd.users.{TOKEN_USER_UUID}.rooms.created',
                    ),
                    headers=has_entries(tenant_uuid=str(TOKEN_TENANT_UUID)),
                ),
                has_entries(
                    message=has_entries(
                        data=has_entries(room_args),
                        required_acl=f'events.chatd.users.{UUID}.rooms.created',
                    ),
                    headers=has_entries(tenant_uuid=str(TOKEN_TENANT_UUID)),
                ),
            ),
        )

        self._delete_room(room)
    def test_that_the_right_credentials_return_a_token_with_datas(self):
        response = self._post_token('foo', 'bar')

        assert_that(
            response,
            has_entries(
                token=uuid_(),
                metadata=has_entries(uuid=self.user['uuid']),
                acl=ANY,
            ),
        )
 def test_minimal_body(self):
     with self.source(self.client, self.valid_body) as source:
         assert_that(
             source,
             has_entries(
                 uuid=uuid_(),
                 name='google',
                 auth=has_entries(host='localhost',
                                  port=443,
                                  verify_certificate=True),
             ),
         )
Beispiel #14
0
    def test_list(self):
        result = self.contacts(self.client, self.source_uuid)
        assert_that(
            result,
            has_entries(
                total=4,
                filtered=4,
                items=contains_inanyorder(
                    has_entries(
                        id=1,
                        uuid=uuid_(),
                        firstname='John',
                        lastname='Doe',
                        exten='1234',
                        voicemail_number=None,
                        mobile_phone_number='+14184765458',
                        email='*****@*****.**',
                    ),
                    has_entries(firstname='Mary'),
                    has_entries(firstname='Bob'),
                    has_entries(firstname='Charles'),
                ),
            ),
        )

        assert_that(
            result['items'][0].keys(),
            contains_inanyorder(
                'id',
                'uuid',
                'firstname',
                'lastname',
                'exten',
                'voicemail_number',
                'mobile_phone_number',
                'email',
            ),
        )

        assert_that(
            calling(self.contacts).with_args(self.client, UNKNOWN_UUID),
            raises(Exception).matching(
                has_properties(response=has_properties(status_code=404))),
        )
    def test_create_minimal_parameters(self, room):
        message_args = {'content': 'Message content'}

        message = self.chatd.rooms.create_message_from_user(
            room['uuid'], message_args)

        assert_that(
            message,
            has_entries(
                uuid=uuid_(),
                content=message_args['content'],
                alias=none(),
                user_uuid=str(TOKEN_USER_UUID),
                tenant_uuid=str(TOKEN_TENANT_UUID),
                wazo_uuid=str(WAZO_UUID),
                created_at=is_not(none()),
                room=has_entries(uuid=room['uuid']),
            ),
        )
 def check():
     response = self.client.conference_source.list(tenant_uuid=self.tenant_uuid)
     key_file = '/var/lib/wazo-auth-keys/wazo-dird-conference-backend-key.yml'
     assert_that(
         response,
         has_entries(
             items=has_item(
                 has_entries(
                     uuid=uuid_(),
                     tenant_uuid=self.tenant_uuid,
                     name='auto_conference_mytenant',
                     auth={
                         'host': 'localhost',
                         'port': 9497,
                         'prefix': None,
                         'https': False,
                         'version': '0.1',
                         'key_file': key_file,
                     },
                     confd={
                         'host': 'localhost',
                         'port': 9486,
                         'prefix': None,
                         'https': False,
                         'version': '1.1',
                     },
                     first_matched_columns=contains_inanyorder(
                         'extensions', 'incalls'
                     ),
                     searched_columns=contains_inanyorder(
                         'name', 'extensions', 'incalls'
                     ),
                     format_columns={
                         'phone': '{extensions[0]}',
                         'reverse': '{name}',
                     },
                 )
             )
         ),
     )
Beispiel #17
0
    def test_valid_post(self):
        body = {
            'name': 'display',
            'columns': [
                {'field': 'fn', 'title': 'Firstname', 'default': ''},
                {
                    'field': 'mobile',
                    'title': 'Mobile',
                    'type': 'number',
                    'number_display': '{firstname} (Mobile)',
                    'default': None,
                },
            ],
        }

        with self.create(self.client, body) as display:
            assert_that(
                display,
                has_entries(
                    uuid=uuid_(),
                    tenant_uuid=MAIN_TENANT,
                    columns=contains(
                        has_entries(
                            field='fn',
                            title='Firstname',
                            default='',
                            type=None,
                            number_display=None,
                        ),
                        has_entries(
                            field='mobile',
                            title='Mobile',
                            type='number',
                            number_display='{firstname} (Mobile)',
                            default=None,
                        ),
                    ),
                ),
            )
Beispiel #18
0
    def test_email_updates_as_user(self, foobar):
        assert_http_error(404, self.client.users.update_emails, UNKNOWN_UUID,
                          [])
        assert_http_error(400, self.client.users.update_emails, foobar['uuid'],
                          [ONE, ONE])

        email_uuid = foobar['emails'][0]['uuid']
        result = self.client.users.update_emails(foobar['uuid'], [ONE, THREE])
        assert_that(
            result,
            contains_inanyorder(
                has_entries(uuid=email_uuid, **ONE),
                has_entries(
                    uuid=uuid_(),
                    address=THREE['address'],
                    main=THREE['main'],
                    confirmed=False,
                ),
                # Confirmed is ignored when modifying as a user
            ),
        )

        result = self.client.users.update_emails(foobar['uuid'], [])
        assert_that(result, empty())
Beispiel #19
0
    def test_post(self, foobar, foobaz, other):
        assert_that(
            other,
            has_entries(
                uuid=uuid_(),
                name=None,
                slug='slug3',
                parent_uuid=self.top_tenant_uuid,
                address=has_entries(**ADDRESS_NULL),
                domain_names=is_(empty()),
            ),
        )
        assert_that(
            foobaz,
            has_entries(
                uuid='6668ca15-6d9e-4000-b2ec-731bc7316767',
                name='foobaz',
                slug='slug2',
                parent_uuid=self.top_tenant_uuid,
                address=has_entries(**ADDRESS_NULL),
                domain_names=contains_inanyorder(*VALID_DOMAIN_NAMES_2),
            ),
        )

        assert_that(
            foobar,
            has_entries(
                uuid=uuid_(),
                name='foobar',
                slug='slug1',
                phone=PHONE_1,
                parent_uuid=self.top_tenant_uuid,
                address=has_entries(**ADDRESS_1),
                domain_names=contains_inanyorder(*VALID_DOMAIN_NAMES_1),
            ),
        )

        wazo_all_users_groups = self.client.groups.list(
            search='wazo-all-users', recurse=True
        )['items']
        assert_that(
            wazo_all_users_groups,
            contains_inanyorder(
                has_entries(
                    name=f'wazo-all-users-tenant-{self.top_tenant_uuid}',
                    tenant_uuid=self.top_tenant_uuid,
                ),
                has_entries(
                    name=f'wazo-all-users-tenant-{foobar["uuid"]}',
                    tenant_uuid=foobar['uuid'],
                ),
                has_entries(
                    name=f'wazo-all-users-tenant-{foobaz["uuid"]}',
                    tenant_uuid=foobaz['uuid'],
                ),
                has_entries(
                    name=f'wazo-all-users-tenant-{other["uuid"]}',
                    tenant_uuid=other['uuid'],
                ),
            ),
        )

        def expected_policies(tenant_uuid):
            return contains_exactly(
                has_entries(
                    slug=ALL_USERS_POLICY_SLUG,
                    tenant_uuid=tenant_uuid,
                    acl=has_item('integration_tests.access'),
                )
            )

        # Assert default policies from admin point of view (recurse=True)
        wazo_all_users_policies = [
            {
                'group': wazo_all_users_group,
                'policies': self.client.groups.get_policies(
                    wazo_all_users_group['uuid'], recurse=True
                )['items'],
            }
            for wazo_all_users_group in wazo_all_users_groups
        ]

        assert_that(
            wazo_all_users_policies,
            contains_inanyorder(
                has_entries(
                    group=has_entries(tenant_uuid=self.top_tenant_uuid),
                    policies=expected_policies(self.top_tenant_uuid),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=foobar['uuid']),
                    policies=expected_policies(self.top_tenant_uuid),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=foobaz['uuid']),
                    policies=expected_policies(self.top_tenant_uuid),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=other['uuid']),
                    policies=expected_policies(self.top_tenant_uuid),
                ),
            ),
        )

        # Assert default policies from tenant point of view
        result = []
        for group in wazo_all_users_groups:
            self.client.tenant_uuid = group['tenant_uuid']
            policies = self.client.groups.get_policies(group['uuid'])['items']
            self.client.tenant_uuid = None
            result.append({'group': group, 'policies': policies})

        assert_that(
            result,
            contains_inanyorder(
                has_entries(
                    group=has_entries(tenant_uuid=self.top_tenant_uuid),
                    policies=expected_policies(self.top_tenant_uuid),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=foobar['uuid']),
                    policies=expected_policies(foobar['uuid']),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=foobaz['uuid']),
                    policies=expected_policies(foobaz['uuid']),
                ),
                has_entries(
                    group=has_entries(tenant_uuid=other['uuid']),
                    policies=expected_policies(other['uuid']),
                ),
            ),
        )

        tenant_uuids = [
            self.top_tenant_uuid,
            foobar['uuid'],
            foobaz['uuid'],
            other['uuid'],
        ]
        slug = ALL_USERS_POLICY_SLUG
        for tenant_uuid in tenant_uuids:
            assert_that(
                self.client.tenants.get_policies(tenant_uuid)['items'],
                has_item(has_entries(slug=slug, tenant_uuid=tenant_uuid)),
            )

        params = {'name': 'subtenant', 'parent_uuid': foobar['uuid']}
        with self.tenant(self.client, **params) as subtenant:
            assert_that(subtenant, has_entries(uuid=uuid_(), **params))
Beispiel #20
0
    def test_post_with_top_tenant_admin(self, isolated):
        args = {
            'username': '******',
            'firstname': 'Alice',
            'email_address': '*****@*****.**',
            'password': '******',
        }

        # User created in our own tenant
        with self.user(self.client, **args) as user:
            assert_that(user, not_(has_key('password')))
            assert_that(
                user,
                has_entries(
                    uuid=uuid_(),
                    username='******',
                    firstname='Alice',
                    lastname=None,
                    enabled=True,
                    tenant_uuid=self.top_tenant_uuid,
                    emails=contains_inanyorder(
                        has_entries(
                            uuid=uuid_(),
                            address='*****@*****.**',
                            main=True,
                            confirmed=True,
                        )),
                ),
            )

            # TODO move this assertion to the user tenant tests
            tenants = self.client.users.get_tenants(user['uuid'])
            assert_that(tenants['items'],
                        has_items(has_entries(uuid=self.top_tenant_uuid)))

            wazo_all_users_group = self.client.groups.list(
                name=f'wazo-all-users-tenant-{self.top_tenant_uuid}',
                tenant_uuid=self.top_tenant_uuid,
            )['items'][0]
            wazo_all_users_group_members = self.client.groups.get_users(
                wazo_all_users_group['uuid'])['items']
            assert_that(
                wazo_all_users_group_members,
                has_item(has_entries(uuid=user['uuid'])),
            )

        # User created in subtenant
        with self.user(self.client, tenant_uuid=isolated['uuid'],
                       **args) as user:
            assert_that(
                user,
                has_entries(
                    uuid=uuid_(),
                    username='******',
                    firstname='Alice',
                    lastname=None,
                    enabled=True,
                    tenant_uuid=isolated['uuid'],
                    emails=contains_inanyorder(
                        has_entries(
                            uuid=uuid_(),
                            address='*****@*****.**',
                            main=True,
                            confirmed=True,
                        )),
                ),
            )

            # TODO move this assertion to the user tenant tests
            tenants = self.client.users.get_tenants(user['uuid'])
            assert_that(
                tenants,
                has_entries(items=contains_exactly(
                    has_entries(uuid=isolated['uuid'])),
                            total=1),
            )

        args = {
            'uuid': 'fcf9724a-15aa-4dc5-af3c-a9acdb6a2ab9',
            'username': '******',
            'email_address': '*****@*****.**',
        }

        with self.user(self.client, **args) as user:
            assert_http_error(409, self.client.users.new, **args)

        assert_http_error(400, self.client.users.new, username='******' * 257)
        with self.user(self.client, username='******' * 256) as user:
            assert_that(user, has_entries(username='******' * 256))

        # User creation with no email address
        with self.user(self.client, username='******') as user:
            assert_that(user, has_entries(username='******', emails=empty()))

        args = {'username': '******', 'email_address': None}
        with self.user(self.client, **args) as user:
            assert_that(user, has_entries(emails=empty()))

        user_args = {
            'username': '******',
            'password': '******',
            'enabled': False
        }
        with self.user(self.client, **user_args) as user:
            assert_that(user, has_entries(enabled=False))
            user_client = self.make_auth_client('foobar', 'foobaz')
            assert_http_error(401, user_client.token.new, 'wazo_user')
Beispiel #21
0
def then_server_has_uuid(context):
    infos = context.confd_client.infos.get()
    assert_that(infos, has_entries(uuid=uuid_()))