tachybase_todo/packages/plugins/acl/src/__tests__/middleware.test.ts
YANG QIA 24601aa66f
feat(auth): support custom authentication (#2007)
* feat(auth): init auth package & collection

* feat(auth): register

* feat(auth): use authenticator

* feat(auth): mapRoles

* feat(auth): refactor

* feat(auth): base auth class

* feat(auth): add plugin

* chore(auth): test

* chore(auth): add test cases

* feat(auth): authenticators pane

* chore(auth): custom hook useAuthTypes

* feat(auth): authenticator pane

* chore(auth): store options schema using context

* feat(auth): signInPage provider

* feat(auth): signUpPage provider

* chore(auth): solve build errors

* chore(auth): add dependency

* chore(auth): remove dependency cycles

* chore(auth): add plugin-auth to preset

* chore(auth): fix test

* feat(auth): authenticator enable status

* fix(test): fix test using new authentication

* feat(auth): migration, set up basic auth

* chore(auth): can set options ui by component

* fix(test): workflow manunal.test

* fix(test): typo

* feat(auth): support multi-language

* chore(auth): imporve code

* chore(auth): hide button if no configuration

* chore(auth): readme

* chore(auth): remove allowSignup prop

* chore(auth): move configure pane to edit form

* fix(auth): jwt options bug

* feat(auth): init sms-auth

* chore(auth): at least authenticator required

* chore(auth): add test

* feat(auth): support sms auth

* fix(auth): fix test

* chore(auth): move findOrCreateUser to AuthModel

* chore(auth): history compatible processing

* feat(auth): support SAML auth

* chore(auth): saml auth list

* chore(saml-auth): improve ui

* Merge branch 'main' into feat/authentication

* chore(auth): improve code

* fix(saml-auth): fix bug

* fix(saml-auth): fix saml options

* chore(saml-auth): compatible processing && ut

* fix(auth): signin page bug

* chore(auth): saml compatible processing

* feat(auth): oidc-auth

* fix(oidc-auth): bug

* fix(oidc-auth): bug

* fix(auth): fix test

* chore(auth): filter enabled authenticator

* chore(oidc): add field map

* chore(auth): update readme

* docs(auth): create sms-auth readme

* feat(auth): allow signup config

* test(auth): fix test

* feat(auth): allow saml and oidc use http

* chore(oidc-auth): extends timeout

* docs(auth): update readme

* feat(auth): support sort

* docs(saml): update readme

* feat(auth): support sort all authenticator

* Merge branch 'main' into feat/authentication

* Merge branch 'main' into feat/authentication

* feat: improve code

* docs(auth): add doc

* Merge branch 'main' into feat/authentication

* chore: update yarn.lock

* feat: improve code

* chore(acl): write role to acl if it exists in database and not found … (#2001)

* chore(acl): write role to acl if it exists in database and not found in acl

* fix: test

* fix: eager load with nested association (#2002)

* chore: upgrade vitest

* chore: edit

* refactor: auth class

* fix: set options

* chore(acl): write role to acl if it exists in database and not found … (#2001)

* chore(acl): write role to acl if it exists in database and not found in acl

* fix: test

* fix: eager load with nested association (#2002)

* chore: upgrade vitest

* chore: add migrations

* test: fix api-client test

* chore: add sms-auth

* feat: avoid no permission after auth type disabled

* fix: translation

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-06-07 23:46:42 +08:00

211 lines
4.9 KiB
TypeScript

import { ACL } from '@nocobase/acl';
import { Database, Model } from '@nocobase/database';
import UsersPlugin from '@nocobase/plugin-users';
import { MockServer } from '@nocobase/test';
import { prepareApp } from './prepare';
describe('middleware', () => {
let app: MockServer;
let role: Model;
let db: Database;
let acl: ACL;
let admin;
let adminAgent;
beforeEach(async () => {
app = await prepareApp();
db = app.db;
acl = app.acl;
role = await db.getRepository('roles').findOne({
filter: {
name: 'admin',
},
});
const UserRepo = db.getCollection('users').repository;
admin = await UserRepo.create({
values: {
roles: ['admin'],
},
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = app.agent().login(admin);
await db.getRepository('collections').create({
values: {
name: 'posts',
},
context: {},
});
await db.getRepository('collections.fields', 'posts').create({
values: {
name: 'title',
type: 'string',
},
context: {},
});
await db.getRepository('collections.fields', 'posts').create({
values: {
name: 'description',
type: 'string',
},
context: {},
});
await db.getRepository('collections.fields', 'posts').create({
values: {
name: 'createdById',
type: 'integer',
},
context: {},
});
});
afterEach(async () => {
await app.destroy();
});
it('should throw 403 when no permission', async () => {
const response = await app.agent().resource('posts').create({
values: {},
});
expect(response.statusCode).toEqual(403);
});
it('should return 200 when role has permission', async () => {
await db.getRepository('roles').update({
filterByTk: 'admin',
values: {
strategy: {
actions: ['create:all'],
},
},
});
const response = await adminAgent.resource('posts').create({
values: {},
});
expect(response.statusCode).toEqual(200);
});
it('should limit fields on view actions', async () => {
await adminAgent.resource('roles.resources', role.get('name')).create({
values: {
name: 'posts',
usingActionsConfig: true,
actions: [
{
name: 'create',
fields: ['title', 'description'],
},
{
name: 'view',
fields: ['title'],
},
],
},
});
await adminAgent.resource('posts').create({
values: {
title: 'post-title',
description: 'post-description',
},
});
const post = await db.getRepository('posts').findOne();
expect(post.get('title')).toEqual('post-title');
expect(post.get('description')).toEqual('post-description');
const response = await adminAgent.resource('posts').list({});
expect(response.statusCode).toEqual(200);
const [data] = response.body.data;
expect(data['id']).not.toBeUndefined();
expect(data['title']).toEqual('post-title');
expect(data['description']).toBeUndefined();
});
it('should parse template value on action params', async () => {
const res = await adminAgent.resource('rolesResourcesScopes').create({
values: {
name: 'own',
scope: {
createdById: '{{ ctx.state.currentUser.id }}',
},
},
});
await adminAgent.resource('roles.resources', role.get('name')).create({
values: {
name: 'posts',
usingActionsConfig: true,
actions: [
{
name: 'create',
fields: ['title', 'description', 'createdById'],
},
{
name: 'view',
fields: ['title'],
scope: res.body.data.id,
},
],
},
});
await adminAgent.resource('posts').create({
values: {
title: 't1',
description: 'd1',
createdById: 1,
},
});
await adminAgent.resource('posts').create({
values: {
title: 't2',
description: 'p2',
createdById: 2,
},
});
const response = await adminAgent.resource('posts').list();
const data = response.body.data;
expect(data.length).toEqual(1);
});
it('should change fields params to whitelist in create action', async () => {
await adminAgent.resource('roles.resources', role.get('name')).create({
values: {
name: 'posts',
usingActionsConfig: true,
actions: [
{
name: 'create',
fields: ['title'],
},
],
},
});
await adminAgent.resource('posts').create({
values: {
title: 'post-title',
description: 'post-description',
},
});
const post = await db.getRepository('posts').findOne();
expect(post.get('title')).toEqual('post-title');
expect(post.get('description')).toBeNull();
});
});