Skip to content

Commit

Permalink
Whitelist is now visible to admin, closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
hjerpbakk committed Jul 7, 2017
1 parent 19d8d54 commit 4943956
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 17 deletions.
17 changes: 17 additions & 0 deletions Hjerpbakk.Profilebot/Commands/ShowWhitelistedUsersCommand.cs
@@ -0,0 +1,17 @@
using System;

namespace Hjerpbakk.Profilebot.Commands {
internal class ShowWhitelistedUsersCommand : ProfileBotCommand {
static readonly Lazy<ShowWhitelistedUsersCommand> instance;

static ShowWhitelistedUsersCommand() {
instance = new Lazy<ShowWhitelistedUsersCommand>(() => new ShowWhitelistedUsersCommand());
}

ShowWhitelistedUsersCommand() { }

public static ShowWhitelistedUsersCommand Create() {
return instance.Value;
}
}
}
19 changes: 16 additions & 3 deletions Hjerpbakk.Profilebot/FaceDetection/FaceWhitelist.cs
Expand Up @@ -41,9 +41,7 @@ public class FaceWhitelist : IFaceWhitelist {
/// <returns>Whether the given user is whitelisted.</returns>
public async Task<bool> IsUserWhitelisted(SlackUser user) {
user.Guard();
if (whitelistedUserIds.Count == 0) {
await PopulateWhitelist();
}
await InitializeWhitelistIfNeeded();

return whitelistedUserIds.Contains(user.Id);
}
Expand Down Expand Up @@ -82,6 +80,15 @@ public class FaceWhitelist : IFaceWhitelist {
await blobRef.UploadTextAsync(report.CreateHTMLReport());
}

/// <summary>
/// Gets the whitelisted users.
/// </summary>
/// <returns>The whitelisted users</returns>
public async Task<SlackUser[]> GetWhitelistedUsers() {
await InitializeWhitelistIfNeeded();
return whitelistedUserIds.Select(id => new SlackUser {Id = id}).ToArray();
}

async Task PopulateWhitelist() {
var blobs = container.ListBlobs();
foreach (var blob in blobs.Cast<CloudBlockBlob>()) {
Expand All @@ -92,5 +99,11 @@ public class FaceWhitelist : IFaceWhitelist {
}
}
}

async Task InitializeWhitelistIfNeeded() {
if (whitelistedUserIds.Count == 0) {
await PopulateWhitelist();
}
}
}
}
6 changes: 6 additions & 0 deletions Hjerpbakk.Profilebot/FaceDetection/IFaceWhitelist.cs
Expand Up @@ -27,5 +27,11 @@ public interface IFaceWhitelist {
/// <param name="report">The report to upload.</param>
/// <returns>No object or value is returned by this method when it completes.</returns>
Task UploadReport(ValidationReport report);

/// <summary>
/// Gets the whitelisted users.
/// </summary>
/// <returns>The whitelisted users</returns>
Task<SlackUser[]> GetWhitelistedUsers();
}
}
1 change: 1 addition & 0 deletions Hjerpbakk.Profilebot/Hjerpbakk.Profilebot.csproj
Expand Up @@ -137,6 +137,7 @@
<Compile Include="Commands\NotifyAllProfilesCommand.cs" />
<Compile Include="Commands\NotifySingleProfileCommand.cs" />
<Compile Include="Commands\AnswerRegularUserCommand.cs" />
<Compile Include="Commands\ShowWhitelistedUsersCommand.cs" />
<Compile Include="Commands\ShowVersionNumberCommand.cs" />
<Compile Include="Commands\UnknownCommand.cs" />
<Compile Include="Commands\ValidateAllProfilesCommand.cs" />
Expand Down
18 changes: 11 additions & 7 deletions Hjerpbakk.Profilebot/MessageParser.cs
Expand Up @@ -17,16 +17,20 @@ internal static class MessageParser {
case "version":
return ShowVersionNumberCommand.Create();
default:
return ParseVerbSubjectCommands(normalizedMessage);
}
}
var commandParts = normalizedMessage.Split(' ');
if (commandParts.Length == 2 && commandParts[1].StartsWith("<@") && commandParts[1][commandParts[1].Length - 1] == '>') {
return ParseVerbSubjectCommands(commandParts);
}

if (normalizedMessage == "whitelist") {
return ShowWhitelistedUsersCommand.Create();
}

static ProfileBotCommand ParseVerbSubjectCommands(string normalizedMessage) {
var commandParts = normalizedMessage.Split(' ');
if (commandParts.Length != 2 || !commandParts[1].StartsWith("<@") || commandParts[1][commandParts[1].Length - 1] != '>') {
return UnknownCommand.Create();
return UnknownCommand.Create();
}
}

static ProfileBotCommand ParseVerbSubjectCommands(string[] commandParts) {
var verb = commandParts[0];
var slackUserId = commandParts[1].Substring(2, commandParts[1].Length - 3).ToUpper();
var subject = new SlackUser {Id = slackUserId};
Expand Down
11 changes: 11 additions & 0 deletions Hjerpbakk.Profilebot/ProfileBotImplmentation.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Hjerpbakk.Profilebot.Commands;
Expand Down Expand Up @@ -102,6 +103,9 @@ public sealed class ProfilebotImplmentation : IDisposable {
case ShowVersionNumberCommand _:
await SendVersionNumber();
break;
case ShowWhitelistedUsersCommand _:
await SendWhitelistedUsers();
break;
default:
await slackIntegration.SendDirectMessage(message.User,
$"Available commands are:{Environment.NewLine}- validate all users{Environment.NewLine}- notify all users{Environment.NewLine}- validate @user{Environment.NewLine}- notify @user{Environment.NewLine}- whitelist @user{Environment.NewLine}- version");
Expand Down Expand Up @@ -215,5 +219,12 @@ public sealed class ProfilebotImplmentation : IDisposable {
var version = Assembly.GetAssembly(typeof(ProfilebotImplmentation)).GetName().Version.ToString();
await slackIntegration.SendDirectMessage(adminUser, version);
}

async Task SendWhitelistedUsers() {
await slackIntegration.IndicateTyping(adminUser);
var whitelistedUsers = await faceWhitelist.GetWhitelistedUsers();
var message = "Whitelist: " + string.Join(", ", whitelistedUsers.Select(u => u.FormattedUserId));
await slackIntegration.SendDirectMessage(adminUser, message);
}
}
}
4 changes: 2 additions & 2 deletions Hjerpbakk.Profilebot/Properties/AssemblyInfo.cs
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: InternalsVisibleTo("Test.Hjerpbakk.Profilebot")]
13 changes: 13 additions & 0 deletions Test.Hjerpbakk.Profilebot/FaceDetection/FaceWhitelistTests.cs
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hjerpbakk.Profilebot.Configuration;
Expand Down Expand Up @@ -127,6 +128,18 @@ public class FaceWhitelistTests : IClassFixture<BlobStorageFixture> {
await VerifyUserIsWhiteListed(userToWhiteList);
}

[Fact]
public async Task GetWhitelistedUsers() {
var faceWhitelist = Create();
var userToWhiteList = new SlackUser {Id = Path.GetRandomFileName()};

await faceWhitelist.WhitelistUser(userToWhiteList);

var whitelistedUsers = await faceWhitelist.GetWhitelistedUsers();

Assert.NotNull(whitelistedUsers.SingleOrDefault(u => u.Id == userToWhiteList.Id));
}

[Fact]
public async Task WhitelistUser_Null_Fails() {
var faceWhitelist = Create();
Expand Down
7 changes: 7 additions & 0 deletions Test.Hjerpbakk.Profilebot/MessageParserTests.cs
Expand Up @@ -110,6 +110,13 @@ public class MessageParserTests {
Assert.IsType<ShowVersionNumberCommand>(command);
}

[Fact]
public void ParseCommand_ShowWhitelistedUsers() {
var command = MessageParser.ParseCommand(CreateMessage(adminUser, "whitelist"), adminUser);

Assert.IsType<ShowWhitelistedUsersCommand>(command);
}

public static SlackMessage CreateMessage(SlackUser sender, string messageText) =>
new SlackMessage {User = sender, Text = messageText, ChatHub = new SlackChatHub {Type = SlackChatHubType.DM}};
}
Expand Down
17 changes: 14 additions & 3 deletions Test.Hjerpbakk.Profilebot/ProfilebotImplmentationTests.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Hjerpbakk.Profilebot.FaceDetection;
using Hjerpbakk.Profilebot;
Expand Down Expand Up @@ -316,12 +315,24 @@ public class ProfilebotImplmentationTests {
[Fact]
public async Task Version_ShowsVersion() {
var creationResult = await CreateProfileBot(true);
var version = Assembly.GetAssembly(typeof(ProfilebotImplmentation)).GetName().Version.ToString();

creationResult.SlackIntegration.Raise(s => s.MessageReceived += null, MessageParserTests.CreateMessage(adminUser, "version"));

creationResult.SlackIntegration.Verify(s => s.IndicateTyping(It.Is<SlackUser>(u => u.Id == adminUser.Id)));
creationResult.SlackIntegration.Verify(s => s.SendDirectMessage(It.Is<SlackUser>(u => u.Id == adminUser.Id), version));
creationResult.SlackIntegration.Verify(s => s.SendDirectMessage(It.Is<SlackUser>(u => u.Id == adminUser.Id), "1.0.0.0"));
}

[Fact]
public async Task Whitelist_ShowWhitelistedUsers() {
var creationResult = await CreateProfileBot(true);
var slackIntegration = creationResult.SlackIntegration;
creationResult.FaceWhitelistFake.Setup(w => w.GetWhitelistedUsers()).ReturnsAsync(new[] {new SlackUser {Id = "U1TBU8336"}, new SlackUser {Id = "U1TBU8346"}});

slackIntegration.Raise(s => s.MessageReceived += null, MessageParserTests.CreateMessage(adminUser, "Whitelist"));

slackIntegration.Verify(s => s.IndicateTyping(It.Is<SlackUser>(u => u.Id == adminUser.Id)));
creationResult.FaceWhitelistFake.Verify(f => f.GetWhitelistedUsers());
slackIntegration.Verify(s => s.SendDirectMessage(It.Is<SlackUser>(u => u.Id == adminUser.Id), "Whitelist: <@U1TBU8336>, <@U1TBU8346>"));
}

static ProfileValidationResult ValidResult() =>
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Expand Up @@ -4,7 +4,7 @@
only:
- master

version: 1.0.2.{build}
version: 1.1.0.{build}

pull_requests:
do_not_increment_build_number: true
Expand Down Expand Up @@ -87,7 +87,7 @@

# Other branches, only build and test
-
version: 1.0.2.{build}
version: 1.1.0.{build}

pull_requests:
do_not_increment_build_number: true
Expand Down

0 comments on commit 4943956

Please sign in to comment.