test(role): write remaining model tests

This commit is contained in:
Ali BARIN
2024-10-22 15:15:48 +00:00
parent bc195ed452
commit a8f4fb7c22
2 changed files with 104 additions and 11 deletions

View File

@@ -109,17 +109,7 @@ class Role extends Base {
return await this.$query().delete();
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
await this.preventAlteringAdmin();
}
async $beforeDelete(queryContext) {
await super.$beforeDelete(queryContext);
await this.preventAlteringAdmin();
async assertNoRoleUserExists() {
const userCount = await this.$relatedQuery('users').limit(1).resultSize();
const hasUsers = userCount > 0;
@@ -135,7 +125,9 @@ class Role extends Base {
type: 'ValidationError',
});
}
}
async assertNoConfigurationUsage() {
const samlAuthProviderUsingDefaultRole = await SamlAuthProvider.query()
.where({
default_role_id: this.id,
@@ -157,6 +149,26 @@ class Role extends Base {
});
}
}
async assertRoleIsNotUsed() {
await this.assertNoRoleUserExists();
await this.assertNoConfigurationUsage();
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
await this.preventAlteringAdmin();
}
async $beforeDelete(queryContext) {
await super.$beforeDelete(queryContext);
await this.preventAlteringAdmin();
await this.assertRoleIsNotUsed();
}
}
export default Role;