package beta import ( "testing" "github.com/bwmarrin/discordgo" ) const ( betaGuild = "1170934850157547600" testerRol = "555000000000000001" ) func member(roles ...string) *discordgo.Member { return &discordgo.Member{Roles: roles} } func interaction(guildID string, m *discordgo.Member) *discordgo.InteractionCreate { return &discordgo.InteractionCreate{Interaction: &discordgo.Interaction{GuildID: guildID, Member: m}} } func TestAllowed(t *testing.T) { cases := []struct { name string guild, role string // configuration inGuild string member *discordgo.Member want bool inBeta, hasRole bool }{ {"tester in the beta server", betaGuild, testerRol, betaGuild, member("1", testerRol), true, true, true}, {"member without the role", betaGuild, testerRol, betaGuild, member("1", "2"), false, true, false}, {"member with no roles at all", betaGuild, testerRol, betaGuild, member(), false, true, false}, {"the role in another server doesn't count", betaGuild, testerRol, "999000000000000001", member(testerRol), false, false, false}, {"a DM has no member", betaGuild, testerRol, "", nil, false, false, false}, {"a nil member in the beta server", betaGuild, testerRol, betaGuild, nil, false, true, false}, {"no role configured: nobody", betaGuild, "", betaGuild, member(testerRol, ""), false, true, false}, {"no beta server configured: nobody", "", testerRol, betaGuild, member(testerRol), false, false, false}, {"a role ID that isn't a number is dropped", betaGuild, "tester-role", betaGuild, member("tester-role"), false, true, false}, {"a server ID that isn't a number is dropped", "my-server", testerRol, "my-server", member(testerRol), false, false, false}, {"a role that only contains the ID isn't a match", betaGuild, testerRol, betaGuild, member(testerRol + "9"), false, true, false}, {"stray spaces in the configuration are ignored", " " + betaGuild + " ", " " + testerRol + "\n", betaGuild, member(testerRol), true, true, true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { Configure(tc.guild, tc.role) if got := Allowed(tc.inGuild, tc.member); got != tc.want { t.Errorf("Allowed = %v, want %v", got, tc.want) } if got := AllowedInteraction(interaction(tc.inGuild, tc.member)); got != tc.want { t.Errorf("AllowedInteraction = %v, want %v", got, tc.want) } st := StatusOf(tc.inGuild, tc.member) if st.InBetaGuild != tc.inBeta || st.HasRole != tc.hasRole { t.Errorf("StatusOf = %+v, want in beta server %v, has role %v", st, tc.inBeta, tc.hasRole) } }) } } func TestNotConfiguredAtAll(t *testing.T) { current.Store(nil) if Allowed(betaGuild, member(testerRol)) || GuildID() != "" { t.Error("before Configure nothing is allowed and there's no beta server") } if AllowedInteraction(nil) || AllowedInteraction(&discordgo.InteractionCreate{}) { t.Error("a missing interaction is never allowed") } } func TestGateRunsTheHandlerOnlyForTesters(t *testing.T) { Configure(betaGuild, testerRol) var answered []string orig := respond respond = func(_ *discordgo.Session, _ *discordgo.InteractionCreate, msg string) { answered = append(answered, msg) } t.Cleanup(func() { respond = orig }) ran := 0 h := Gate(func(*discordgo.Session, *discordgo.InteractionCreate) { ran++ }) h(nil, interaction(betaGuild, member(testerRol))) if ran != 1 || len(answered) != 0 { t.Fatalf("a tester runs the handler without being refused: ran=%d answered=%v", ran, answered) } for name, i := range map[string]*discordgo.InteractionCreate{ "no role": interaction(betaGuild, member("1")), "wrong server": interaction("999000000000000001", member(testerRol)), "DM": interaction("", nil), } { before := ran h(nil, i) if ran != before { t.Errorf("%s: the handler must not run", name) } } if len(answered) != 3 || answered[0] != denied { t.Errorf("each refusal is answered with the beta message: %v", answered) } } func TestGuardOnItsOwn(t *testing.T) { Configure(betaGuild, testerRol) n := 0 orig := respond respond = func(*discordgo.Session, *discordgo.InteractionCreate, string) { n++ } t.Cleanup(func() { respond = orig }) if !Guard(nil, interaction(betaGuild, member(testerRol))) || n != 0 { t.Error("a tester passes without a reply") } if Guard(nil, interaction(betaGuild, member())) || n != 1 { t.Error("someone else is refused with a reply") } }