package com.example.tests;
import com.frotty27.hrtk.api.annotation.HytaleSuite;
import com.frotty27.hrtk.api.annotation.HytaleTest;
import com.frotty27.hrtk.api.annotation.Tag;
import com.frotty27.hrtk.api.annotation.DisplayName;
import com.frotty27.hrtk.api.annotation.Order;
import com.frotty27.hrtk.api.assert_.HytaleAssert;
import com.frotty27.hrtk.api.assert_.PermissionsAssert;
import com.frotty27.hrtk.api.context.HytaleTestContext;
import com.frotty27.hrtk.api.lifecycle.IsolationStrategy;
@HytaleSuite(value = "Permission Surface Tests", isolation = IsolationStrategy.NONE)
@Tag("permissions")
public class PermissionSurfaceTests {
@HytaleTest
@Order(1)
@DisplayName("Grant a permission and verify it is active")
void grantAndVerifyPermission(HytaleTestContext ctx) {
// Create a mock sender with a name but no permissions.
Object sender = ctx.createCommandSender("moderator");
// Grant a specific permission node.
ctx.grantPermission(sender, "hytale.command.kick");
// assertHasPermission fails if the sender does not have the permission.
PermissionsAssert.assertHasPermission(sender, "hytale.command.kick");
}
@HytaleTest
@Order(2)
@DisplayName("Ungranted permission is absent")
void ungrantedPermissionIsAbsent(HytaleTestContext ctx) {
Object sender = ctx.createCommandSender("regular_player");
ctx.grantPermission(sender, "mymod.use");
// The sender has mymod.use but NOT mymod.admin.
PermissionsAssert.assertHasPermission(sender, "mymod.use");
PermissionsAssert.assertNoPermission(sender, "mymod.admin");
}
@HytaleTest
@Order(3)
@DisplayName("Revoke a permission and verify it is gone")
void revokePermission(HytaleTestContext ctx) {
Object sender = ctx.createCommandSender("temp_admin");
ctx.grantPermission(sender, "hytale.command.ban");
PermissionsAssert.assertHasPermission(sender, "hytale.command.ban");
// Revoke and verify the permission is no longer present.
ctx.revokePermission(sender, "hytale.command.ban");
PermissionsAssert.assertNoPermission(sender, "hytale.command.ban");
}
@HytaleTest
@Order(4)
@DisplayName("Clear all permissions from a sender")
void clearAllPermissions(HytaleTestContext ctx) {
Object sender = ctx.createCommandSender("admin");
ctx.grantPermission(sender, "perm.one");
ctx.grantPermission(sender, "perm.two");
ctx.grantPermission(sender, "perm.three");
// clearPermissions removes everything.
ctx.clearPermissions(sender);
PermissionsAssert.assertNoPermission(sender, "perm.one");
PermissionsAssert.assertNoPermission(sender, "perm.two");
PermissionsAssert.assertNoPermission(sender, "perm.three");
}
}