Skip to content

Commit

Permalink
Merge pull request #1324 from longguikeji/feature-402
Browse files Browse the repository at this point in the history
feat: 🎸 增加了测试用例
  • Loading branch information
hanbinloop authored Oct 8, 2022
2 parents d046c23 + 9c975f9 commit 1e7f466
Show file tree
Hide file tree
Showing 13 changed files with 736 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/v1/views/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_tenant(request, data:TenantCreateIn):
def update_tenant(request, id: str, data:TenantUpdateIn):
""" 编辑租户
"""
tenant = get_object_or_404(Tenant.expand_objects,id=id)
tenant = get_object_or_404(Tenant.valid_objects,id=id)
for attr, value in data.dict().items():
setattr(tenant, attr, value)
tenant.save()
Expand Down
4 changes: 2 additions & 2 deletions api/v1/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def user_fields(request, tenant_id: str):
verbose_names.append(verbose_name)
break
return {"data":items}
# ------------- 获取用户接口 --------------


# ------------- 获取用户接口 --------------
@api.get("/tenant/{tenant_id}/users/{id}/",response=UserOut, tags=['用户'])
@operation(UserOut,roles=[TENANT_ADMIN, PLATFORM_ADMIN])
def get_user(request, tenant_id: str,id:str):
Expand Down
2 changes: 1 addition & 1 deletion api/v1/views/user_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def group_users_add(request, tenant_id: str, user_group_id: str, data: UserGroup
return ErrorDict(ErrorCode.OK)


@api.post("/tenant/{tenant_id}/user_groups/{user_group_id}/users/", tags=['用户分组'])
@api.post("/tenant/{tenant_id}/user_groups/{user_group_id}/batch_users/", tags=['用户分组'])
@operation(roles=[TENANT_ADMIN, PLATFORM_ADMIN])
def group_batch_users_remove(request, tenant_id: str, user_group_id: str, data: UserGroupUserIn):
'''
Expand Down
65 changes: 62 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import Client, TestCase as django_TestCase
from arkid.core.models import *
from arkid.extension.models import *
from webhook.models import *
from arkid.core.token import generate_token

import uuid
Expand Down Expand Up @@ -139,7 +140,7 @@ def setUp(self):
config.save()
self.auto_auth = config
# 一个插件
self.extension = Extension.active_objects.first()
self.extension = Extension.active_objects.order_by('-created').first()
# 主题
config = TenantExtensionConfig()
config.tenant = self.tenant
Expand Down Expand Up @@ -202,5 +203,63 @@ def setUp(self):
self.permission = permission
# 用户分组
self.user_group = UserGroup.valid_objects.filter(tenant=self.tenant).first()

self.register_config = TenantExtensionConfig.valid_objects.filter(extension__package='com.longgui.auth.factor.password', tenant=self.tenant).first()
# 注册配置
self.register_config = TenantExtensionConfig.valid_objects.filter(extension__package='com.longgui.auth.factor.password', tenant=self.tenant).first()
# 数据同步
config = TenantExtensionConfig()
config.tenant = self.tenant
config.extension = Extension.active_objects.get(package='com.longgui.scim.sync.arkid')
config.config = {
"user_url":"",
"group_url":"",
"mode":"server"
}
config.name = '一个新服务配置'
config.type = 'ArkID'
config.save()
self.scim_sync = config
# 飞书可能会没有
self.feishu = TenantExtensionConfig.valid_objects.filter(extension__package='com.longgui.external.idp.feishu', tenant=self.tenant).first()
# 新租户
self.create_tenant = Tenant.objects.create(name='testttt', slug='testttt', icon='')
# 第三方认证
config = TenantExtensionConfig()
config.tenant = self.tenant
config.extension = Extension.active_objects.get(package='com.longgui.external.idp.dingding')
config.config = {
"app_key":"aa",
"app_secret":"bb",
"img_url":"",
"login_url":"",
"callback_url":"",
"bind_url":"",
"frontend_callback":""
}
config.name = 'ssst'
config.type = 'dingding'
config.save()
self.third_auth = config
# 用户分组
user_group = UserGroup.objects.create(tenant=self.tenant, name='一个新分组')
systempermission = SystemPermission()
systempermission.name = user_group.name
systempermission.code = 'group_{}'.format(uuid.uuid4())
systempermission.tenant = self.tenant
systempermission.category = 'group'
systempermission.is_system = True
systempermission.operation_id = ''
systempermission.describe = {}
systempermission.save()
user_group.permission = systempermission
user_group.save()
user_group.users.add(self.user)
self.user_group = user_group
# 一个新用户
create_user = User.objects.create(username='sssss', tenant=self.tenant)
self.create_user = create_user
# webhook
webhook = Webhook.objects.create(tenant=self.tenant, name='xxx', url='https://www.baidu.com', secret='xxxx')
self.webhook = webhook
# webhook 历史记录
webhook_triggerhistory = WebhookTriggerHistory.objects.create(tenant=self.tenant, webhook=self.webhook, request='', response='')
self.webhook_triggerhistory = webhook_triggerhistory
79 changes: 79 additions & 0 deletions tests/test_scim_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from tests import TestCase

class TestScimSyncApi(TestCase):

def test_list_scim_syncs(self):
'''
用户数据同步配置列表
'''
url = '/api/v1/tenant/{}/scim_syncs/'.format(self.tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_create_scim_sync(self):
'''
创建用户数据同步配置
'''
url = '/api/v1/tenant/{}/scim_syncs/'.format(self.tenant.id)
body = {
"type":"ArkID",
"config":{
"user_url":"",
"group_url":"",
"mode":"server"
},
"name":"一个新配置",
"package":"com.longgui.scim.sync.arkid"
}
resp = self.client.post(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_get_scim_sync(self):
'''
获取用户数据同步配置
'''
url = '/api/v1/tenant/{}/scim_syncs/{}/'.format(self.tenant.id, self.scim_sync.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_update_scim_sync(self):
'''
编辑用户数据同步配置
'''
url = '/api/v1/tenant/{}/scim_syncs/{}/'.format(self.tenant.id, self.scim_sync.id)
body = {
"type":"ArkID",
"config":{
"user_url":"",
"group_url":"",
"mode":"server"
},
"name":"一个新配置",
"package":"com.longgui.scim.sync.arkid"
}
resp = self.client.put(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_delete_scim_sync(self):
'''
删除用户数据同步配置
'''
url = '/api/v1/tenant/{}/scim_syncs/{}/'.format(self.tenant.id, self.scim_sync.id)
resp = self.client.delete(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_list_scim_servers(self):
'''
用户数据同步配置列表
'''
url = '/api/v1/tenant/{}/scim_server_list/'.format(self.tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_start_scim_sync(self):
'''
编辑用户数据同步配置
'''
url = '/api/v1/tenant/{}/scim_syncs/{}/sync_start/'.format(self.tenant.id, self.scim_sync.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())
12 changes: 12 additions & 0 deletions tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tests import TestCase

class TestStatisticsApi(TestCase):

def test_get_statistics_charts(self):
'''
获取统计图表
'''
url = '/api/v1/tenant/{}/statistics_charts'.format(self.tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

100 changes: 100 additions & 0 deletions tests/test_tenant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from tests import TestCase

class TestTenantApi(TestCase):

def test_list_tenants(self):
'''
获取租户列表
'''
url = '/api/v1/tenants/'
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_get_tenant(self):
'''
获取租户
'''
url = '/api/v1/tenants/{}/'.format(self.tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_create_tenant(self):
'''
创建租户
'''
url = '/api/v1/tenants/'
body = {
'name': '创建1',
'slug': 'slug11',
'icon': ''
}
resp = self.client.post(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_update_tenant(self):
'''
编辑租户
'''
url = '/api/v1/tenants/{}/'.format(self.create_tenant.id)
body = {
'name': '创建11',
}
resp = self.client.post(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_delete_tenant(self):
'''
删除租户
'''
url = '/api/v1/tenants/{}/'.format(self.create_tenant.id)
resp = self.client.delete(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_get_tenant_config(self):
'''
获取租户配置
'''
url = '/api/v1/tenants/{}/config/'.format(self.create_tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_update_tenant_config(self):
'''
编辑租户配置
'''
url = '/api/v1/tenants/{}/config/'.format(self.create_tenant.id)
body = {
'name': '创建1',
'slug': 'slug11',
'icon': '',
'token_duration_minutes': 1440
}
resp = self.client.post(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_default_tenant(self):
'''
获取当前域名下的默认租户(如无slug则为平台租户)
'''
url = '/api/v1/default_tenant/'.format(self.create_tenant.id)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_logout_tenant(self):
'''
注销租户
'''
url = '/api/v1/tenants/{}/logout/'.format(self.create_tenant.id)
body = {
'password': 'admin',
}
resp = self.client.post(url, body ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())

def test_get_tenant_by_slug(self):
'''
获取租户slug
'''
url = '/api/v1/tenants/tenant_by_slug/{}/'.format(self.create_tenant.slug)
resp = self.client.get(url ,content_type='application/json')
self.assertEqual(resp.status_code, 200, resp.content.decode())
Loading

0 comments on commit 1e7f466

Please sign in to comment.