Example #1
0
        public void ShouldRedirectToStartPathIfUserIsNotAdmin()
        {
            // arrange
            var attribute = new RequireAdminAttribute {
                IsAdministrator = false
            };
            var httpActionContext = new HttpActionContext {
                ControllerContext = new HttpControllerContext {
                    Request = new HttpRequestMessage()
                }
            };

            // act
            attribute.OnAuthorization(httpActionContext);

            // assert
            httpActionContext.Response.Should().NotBeNull();
            httpActionContext.Response.StatusCode.Should().Be(System.Net.HttpStatusCode.Unauthorized);
            httpActionContext.Response.Headers.Count().Should().Be(0);
            var error = httpActionContext.Response.Content as ObjectContent <HttpError>;

            error.Should().NotBeNull();
            var errorValue = error.Value as HttpError;

            errorValue.Should().NotBeNull();
            errorValue.Message.Should().Be(Texts.PermissionIsDenied);
        }
        public async Task Warns(CommandContext ctx, [RemainingText] string suffix)
        {
            List <ulong> userIds = string.IsNullOrWhiteSpace(suffix) ?
                                   new List <ulong>()
            {
                ctx.User.Id
            } :
            Utils.GetIdListFromMessage(ctx.Message.MentionedUsers, suffix);

            if (userIds.Count == 0)
            {
                await CTX.RespondSanitizedAsync(ctx, "Please mention or type a user ID.");

                return;
            }
            if (ctx.Guild.Members.ContainsKey(userIds[0]))
            {
                RequireAdminAttribute adminCheck =
                    new RequireAdminAttribute("Only server admins are allowed to view warnings of other users.");
                if (ctx.User.Id != userIds[0] && !await adminCheck.ExecuteCheckAsync(ctx, false))
                {
                    return;
                }
                DiscordEmbedBuilder warnEmbed = Utils.BaseEmbedBuilder(ctx,
                                                                       $"Warnings for {ctx.Guild.Members[userIds[0]].DisplayName} ({userIds[0]})",
                                                                       ctx.Guild.Members[userIds[0]].AvatarUrl,
                                                                       null, ctx.Guild.Members[userIds[0]].Color);

                var warns = ShimakazeBot.DbCtx.GuildWarn.Where(g =>
                                                               g.UserId == userIds[0] && g.GuildId == ctx.Guild.Id
                                                               ).ToList();

                if (warns.Count() == 0)
                {
                    warnEmbed.WithDescription($"{ctx.Guild.Members[userIds[0]].DisplayName} has no warnings.");
                }
                else
                {
                    warns.ForEach(item => warnEmbed.AddField(item.TimeStamp.ToString(),
                                                             item.WarnMessage.Length > 1024 ? $"{item.WarnMessage.Take(1021)}..." : item.WarnMessage));
                }

                await CTX.RespondSanitizedAsync(ctx, null, false, warnEmbed);
            }
            else
            {
                int       id   = (int)userIds[0];
                GuildWarn warn = await ShimakazeBot.DbCtx.GuildWarn.FindAsync(id);

                if (warn != null)
                {
                    await CTX.RespondSanitizedAsync(ctx, $"{warn.TimeStamp} - {warn.WarnMessage}");
                }
                else
                {
                    await CTX.RespondSanitizedAsync(ctx, $"Unable to find member or ID **{id}**");
                }
            }
        }
Example #3
0
        public void ShouldNotRedirectToStartPathIfUserIsAdmin()
        {
            // arrange
            var attribute = new RequireAdminAttribute {
                IsAdministrator = true
            };
            var httpActionContext = new HttpActionContext {
                ControllerContext = new HttpControllerContext {
                    Request = new HttpRequestMessage()
                }
            };

            // act
            attribute.OnAuthorization(httpActionContext);

            // assert
            httpActionContext.Response.Should().BeNull();
        }