|
| 1 | +/* |
| 2 | + * Copyright 2025 Björn Kautler |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.kautler.command.integ.test.jda.restriction |
| 18 | + |
| 19 | +import jakarta.enterprise.context.ApplicationScoped |
| 20 | +import jakarta.enterprise.event.ObservesAsync |
| 21 | +import jakarta.enterprise.inject.Produces |
| 22 | +import jakarta.enterprise.inject.Vetoed |
| 23 | +import net.dv8tion.jda.api.entities.Message |
| 24 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel |
| 25 | +import net.kautler.command.api.CommandContext |
| 26 | +import net.kautler.command.api.CommandContextTransformer |
| 27 | +import net.kautler.command.api.CommandContextTransformer.InPhase |
| 28 | +import net.kautler.command.api.CommandHandler |
| 29 | +import net.kautler.command.api.annotation.RestrictedTo |
| 30 | +import net.kautler.command.api.event.jda.CommandNotAllowedEventJda |
| 31 | +import net.kautler.command.api.restriction.Restriction |
| 32 | +import net.kautler.command.integ.test.jda.PingIntegTest |
| 33 | +import net.kautler.command.integ.test.spock.AddBean |
| 34 | +import spock.lang.ResourceLock |
| 35 | +import spock.lang.Specification |
| 36 | +import spock.lang.Subject |
| 37 | +import spock.util.concurrent.BlockingVariable |
| 38 | + |
| 39 | +import static java.util.UUID.randomUUID |
| 40 | +import static net.kautler.command.api.CommandContextTransformer.Phase.BEFORE_PREFIX_COMPUTATION |
| 41 | + |
| 42 | +@Subject(CommandHandler) |
| 43 | +class ProducedRestrictionIntegTest extends Specification { |
| 44 | + @AddBean(RestrictionProducer) |
| 45 | + @AddBean(PingCommand) |
| 46 | + @AddBean(IgnoreOtherTestsTransformer) |
| 47 | + @ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.PingCommand.alias') |
| 48 | + @ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.PingCommand.commandNotAllowedEventReceived') |
| 49 | + @ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.IgnoreOtherTestsTransformer.expectedContent') |
| 50 | + def 'produced restrictions should be usable'(TextChannel textChannelAsUser) { |
| 51 | + given: |
| 52 | + PingCommand.alias = "ping_${randomUUID()}" |
| 53 | + IgnoreOtherTestsTransformer.expectedContent = "!${PingCommand.alias}" |
| 54 | + |
| 55 | + and: |
| 56 | + def commandNotAllowedEventReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double) |
| 57 | + PingCommand.commandNotAllowedEventReceived = commandNotAllowedEventReceived |
| 58 | + |
| 59 | + when: |
| 60 | + textChannelAsUser |
| 61 | + .sendMessage(IgnoreOtherTestsTransformer.expectedContent) |
| 62 | + .complete() |
| 63 | + |
| 64 | + then: |
| 65 | + commandNotAllowedEventReceived.get() |
| 66 | + } |
| 67 | + |
| 68 | + @Vetoed |
| 69 | + @ApplicationScoped |
| 70 | + @RestrictedTo(Restriction1) |
| 71 | + static class PingCommand extends PingIntegTest.PingCommand { |
| 72 | + static volatile String alias |
| 73 | + static volatile commandNotAllowedEventReceived |
| 74 | + |
| 75 | + @Override |
| 76 | + List<String> getAliases() { |
| 77 | + [alias] |
| 78 | + } |
| 79 | + |
| 80 | + void handleCommandNotAllowedEvent(@ObservesAsync CommandNotAllowedEventJda commandNotAllowedEvent) { |
| 81 | + commandNotAllowedEventReceived?.set(commandNotAllowedEvent) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static class Restriction1 implements Restriction<Message> { |
| 86 | + @Override |
| 87 | + boolean allowCommand(CommandContext<? extends Message> commandContext) { |
| 88 | + false |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Vetoed |
| 93 | + @ApplicationScoped |
| 94 | + static class RestrictionProducer { |
| 95 | + @Produces |
| 96 | + @ApplicationScoped |
| 97 | + Restriction<Message> restriction = new Restriction1() |
| 98 | + } |
| 99 | + |
| 100 | + @Vetoed |
| 101 | + @ApplicationScoped |
| 102 | + @InPhase(BEFORE_PREFIX_COMPUTATION) |
| 103 | + static class IgnoreOtherTestsTransformer implements CommandContextTransformer<Object> { |
| 104 | + static volatile expectedContent |
| 105 | + |
| 106 | + @Override |
| 107 | + <T> CommandContext<T> transform(CommandContext<T> commandContext, Phase phase) { |
| 108 | + (commandContext.messageContent == expectedContent) |
| 109 | + ? commandContext |
| 110 | + : commandContext.withPrefix('<do not match>').build() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments