tachybase_todo/packages/plugins/acl/src/__tests__/own.test.ts
ChengLei Shao 24ea83f0ff
Feat/create nocobase app (#273)
* create-nocobase-app template from [develop]

* change create-nocobase-app package.json config

* feat: load configuration from directory

* feat: configuration repository toObject

* feat: create application from configuration dir

* feat: application factory with plugins options

* export type

* feat: read application config &  application with plugins options

* feat: release command

* fix: database release

* chore: workflow package.json

* feat: nocobase cli package

* feat: console command

* chore: load application in command

* fix: load packages from process.cwd

* feat: cli load env file

* feat: create-nocobase-app

* fix: gitignore create-nocobase-app lib

* fix: sqlite path

* feat: create plugin

* chore: plugin files template

* chore: move cli into application

* chore: create-nocobase-app

* fix: create plugin

* chore: app-client && app-server

* chore: package.json

* feat: create-nocobase-app download template from npm

* chore: create-nocobase-app template

* fix: config of plugin-users

* fix: yarn.lock

* fix: database build error

* fix: yarn.lock

* fix: resourcer config

* chore: cross-env

* chore: app-client dependents

* fix: env

* chore: v0.6.0-alpha.1

* chore: verdaccio

* chore(versions): 😊 publish v0.6.0

* chore(versions): 😊 publish v0.6.1-alpha.0

* chore(versions): 😊 publish v0.6.2-alpha.0

* chore(versions): 😊 publish v0.6.2-alpha.1

* chore: 0.6.2-alpha.2

* feat: workspaces

* chore(versions): 😊 publish v0.6.2-alpha.3

* chore(versions): 😊 publish v0.6.2-alpha.4

* chore: create-nocobase-app

* chore: create-nocobase-app lib

* fix: update tsconfig.jest.json

* chore: .env

* chore(versions): 😊 publish v0.6.2-alpha.5

* chore(versions): 😊 publish v0.6.2-alpha.6

* feat: improve code

* chore(versions): 😊 publish v0.6.2-alpha.7

* fix: cleanup

* chore(versions): 😊 publish v0.6.2-alpha.8

* chore: tsconfig for app server package

* fix: move files

* fix: move files

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-04-17 10:00:42 +08:00

141 lines
3.4 KiB
TypeScript

import { mockServer, MockServer } from '@nocobase/test';
import { Database } from '@nocobase/database';
import { ACL } from '@nocobase/acl';
import { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import PluginUiSchema from '@nocobase/plugin-ui-schema-storage';
import PluginCollectionManager from '@nocobase/plugin-collection-manager';
import PluginACL from '@nocobase/plugin-acl';
import PluginUser from '@nocobase/plugin-users';
import supertest from 'supertest';
describe('own test', () => {
let app: MockServer;
let db: Database;
let acl: ACL;
let pluginUser: PluginUser;
let adminToken: string;
let userToken: string;
let admin;
let user;
let role;
let agent;
afterEach(async () => {
await app.destroy();
});
beforeEach(async () => {
app = mockServer({
registerActions: true,
});
await app.cleanDb();
app.plugin(PluginUiSchema);
app.plugin(PluginCollectionManager);
app.plugin(PluginUser, {
jwt: {
secret: process.env.JWT_SECRET || '09f26e402586e2faa8da4c98a35f1b20d6b033c60',
},
});
app.plugin(PluginACL);
await app.loadAndInstall();
db = app.db;
const PostCollection = db.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{ type: 'belongsToMany', name: 'tags' },
],
createdBy: true,
});
const TagCollection = db.collection({
name: 'tags',
fields: [
{ type: 'string', name: 'name' },
{ type: 'belongsToMany', name: 'posts' },
],
createdBy: true,
});
const TestCollection = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name' }],
});
await app.db.sync();
agent = supertest.agent(app.callback());
acl = app.acl;
role = await db.getRepository('roles').findOne({
filter: {
name: 'admin',
},
});
admin = await db.getRepository('users').findOne();
pluginUser = app.getPlugin('@nocobase/plugin-users');
adminToken = pluginUser.jwtService.sign({ userId: admin.get('id') });
user = await db.getRepository('users').create({
values: {
nickname: 'test',
roles: ['admin'],
},
});
userToken = pluginUser.jwtService.sign({ userId: user.get('id') });
});
it('should list without createBy', async () => {
let response = await agent
.patch('/roles/admin')
.send({
strategy: {
actions: ['view:own'],
},
})
.set({ Authorization: 'Bearer ' + adminToken });
response = await agent.get('/tests:list').set({ Authorization: 'Bearer ' + userToken });
expect(response.statusCode).toEqual(200);
});
it('should delete with createdBy', async () => {
let response = await agent
.patch('/roles/admin')
.send({
strategy: {
actions: ['view:own', 'create', 'destroy:own'],
},
})
.set({ Authorization: 'Bearer ' + adminToken });
response = await agent
.get('/posts:create')
.send({
title: 't1',
})
.set({ Authorization: 'Bearer ' + userToken });
expect(response.statusCode).toEqual(200);
const data = response.body;
const id = data.data['id'];
response = await agent.delete(`/posts/${id}`).set({ Authorization: 'Bearer ' + userToken });
expect(response.statusCode).toEqual(200);
expect(await db.getRepository('posts').count()).toEqual(0);
});
});