Skip to content

Commit

Permalink
Merge branch 'w652-multi-tenant-export'
Browse files Browse the repository at this point in the history
  • Loading branch information
fblackburn1 committed Apr 16, 2019
2 parents e1f35ce + d3f2ee3 commit 4a5f545
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
-----

* The `/1.1/users/import` PUT has been disabled since it creates invalid configurations
* The `/1.1/users/export` resource will now only list users from the specified tenant.


19.05
Expand Down
33 changes: 33 additions & 0 deletions integration_tests/suite/base/test_user_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from hamcrest import (
assert_that,
contains,
has_entries,
has_item,
)
Expand All @@ -16,8 +17,14 @@
database,
fixtures,
)
from ..helpers.config import (
MAIN_TENANT,
SUB_TENANT,
)
from . import confd_csv, auth, db

UNKNOWN_TENANT = '00000000-0000-0000-0000-000000000000'


@database.reset(db)
@fixtures.user(firstname="Ûrsule",
Expand Down Expand Up @@ -62,6 +69,32 @@ def test_given_user_with_no_associations_when_exporting_then_csv_has_all_user_fi
)))


@database.reset(db)
@fixtures.user(firstname='main', wazo_tenant=MAIN_TENANT)
@fixtures.user(firstname='sub', wazo_tenant=SUB_TENANT)
def test_given_user_in_another_tenant(user_main, user_sub):
auth.users.new(uuid=user_main['uuid'], tenant_uuid=MAIN_TENANT)
auth.users.new(uuid=user_sub['uuid'], tenant_uuid=SUB_TENANT)

response = confd_csv.users.export.get()
assert_that(response.csv(), contains(
has_entries(uuid=user_main['uuid']),
))

response = confd_csv.users.export.get(wazo_tenant=MAIN_TENANT)
assert_that(response.csv(), contains(
has_entries(uuid=user_main['uuid']),
))

response = confd_csv.users.export.get(wazo_tenant=SUB_TENANT)
assert_that(response.csv(), contains(
has_entries(uuid=user_sub['uuid']),
))

response = confd_csv.users.export.get(wazo_tenant=UNKNOWN_TENANT)
response.assert_status(401)


@database.reset(db)
@fixtures.user()
@fixtures.voicemail(name="Jàmie",
Expand Down
3 changes: 2 additions & 1 deletion xivo_confd/database/user_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'call_permissions')


def export_query(separator=";"):
def export_query(tenant_uuid, separator=";"):
ordered_incalls = aliased(
Session.query(
Incall.exten.label('exten'),
Expand Down Expand Up @@ -156,6 +156,7 @@ def export_query(separator=";"):
User.id == grouped_incalls.c.user_id)
.outerjoin(grouped_call_permissions,
User.id == grouped_call_permissions.c.user_id)
.filter(User.tenant_uuid == tenant_uuid)
)

return COLUMNS, query
2 changes: 2 additions & 0 deletions xivo_confd/plugins/user_import/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ paths:
- users
produces:
- text/csv; charset=utf-8
parameters:
- $ref: '#/parameters/tenantuuid'
responses:
'200':
description: Users exported successfully
Expand Down
3 changes: 2 additions & 1 deletion xivo_confd/plugins/user_import/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def __init__(self, service):

@required_acl('confd.users.export.read')
def get(self):
csv_header, users = self.service.export()
tenant = Tenant.autodetect()
csv_header, users = self.service.export(tenant.uuid)
return {
'headers': csv_header,
'content': users,
Expand Down
6 changes: 3 additions & 3 deletions xivo_confd/plugins/user_import/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def __init__(self, user_export_dao, auth_client):
self._user_export_dao = user_export_dao
self._auth_client = auth_client

def export(self):
csv_header, users = self._user_export_dao.export_query()
def export(self, tenant_uuid):
csv_header, users = self._user_export_dao.export_query(tenant_uuid)
users = list(self._format_users(csv_header, users))

wazo_users = self._auth_client.users.list()['items']
wazo_users = self._auth_client.users.list(tenant_uuid=tenant_uuid)['items']
wazo_users = {user['uuid']: user for user in wazo_users}
for user in users:
wazo_user = wazo_users[user['uuid']]
Expand Down
11 changes: 9 additions & 2 deletions xivo_confd/representations/csv_.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Avencall
# Copyright 2016-2019 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

from cStringIO import StringIO
from flask import make_response
from flask import (
json,
make_response,
)
from xivo.unicode_csv import UnicodeDictWriter


def output_csv(data, code, http_headers=None):
# A 401 might happen when trying to find the tenant if the specified tenant is not authorized
if code == 401:
return make_response(json.dumps(data), code)

csv_headers = data['headers']
csv_entries = data['content']

Expand Down

0 comments on commit 4a5f545

Please sign in to comment.