WIP pass the failed tests.

This commit is contained in:
Ahmed Bouhuolia
2020-05-17 07:08:12 +02:00
parent 00de156c9f
commit 10f636d2bc
77 changed files with 2164 additions and 1403 deletions

View File

@@ -1,46 +1,23 @@
import { create, expect } from '~/testInit';
import User from '@/models/User';
import User from '@/models/TenantUser';
import '@/models/Role';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: User', () => {
describe('relations', () => {
it('User model may has many associated roles.', async () => {
const userHasRole = await create('user_has_role');
await create('user_has_role', { user_id: userHasRole.user_id });
const userHasRole = await tenantFactory.create('user_has_role');
await tenantFactory.create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.query().where('id', userHasRole.userId).first();
const userModel = await User.tenant().query().where('id', userHasRole.userId).first();
const userRoles = await userModel.$relatedQuery('roles');
expect(userRoles).to.have.lengthOf(1);
});
});
describe('hasPermissions', () => {
it('Should return true in case user has the given permissions.', async () => {
const resource = await create('resource');
const permission = await create('permission');
const roleHasPerms = await create('role_has_permission', {
resource_id: resource.id,
permission_id: permission.id,
});
const userHasRole = await create('user_has_role', { role_id: roleHasPerms.role_id });
await create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.where('id', userHasRole.user_id).fetch();
const hasPermission = await userModel.hasPermissions(resource.name, [permission.name]);
expect(hasPermission).to.equals(true);
});
it('Should return false in case user has no the given permissions.', async () => {
const roleHasPerms = await create('role_has_permission');
const userHasRole = await create('user_has_role', { role_id: roleHasPerms.role_id });
await create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.where('id', userHasRole.user_id).fetch();
const hasPermission = await userModel.hasPermissions('resource', ['permission']);
expect(hasPermission).to.equals(false);
});
});
});