Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

Commit

Permalink
Feature/jo more refactoring (#36)
Browse files Browse the repository at this point in the history
* Renamed a few method

* Adjusted the code around the pin. so the profile is not used before the pin is entered. This way, it is harder to inject someones profile into another users session.

* var.
  • Loading branch information
jeffrey-opdam committed Jun 6, 2017
1 parent be24c95 commit ac4bdc8
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 164 deletions.
6 changes: 3 additions & 3 deletions src/Team-Services-Bot.AcceptanceTests/CommonSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public void GivenTheUserHasPreviouslyLoggedInIntoTheAccountAndTeamProject(KeyVal

var userData = Config.BotState.GetUserData(ChannelIds.Directline, Config.UserName);

userData.SetCurrentAccount(Config.Account);
userData.SetCurrentProfile(profile);
userData.SetAccount(Config.Account);
userData.SetProfile(profile);
userData.SetProfiles(new List<VstsProfile> { profile });
userData.SetCurrentTeamProject(pair.Value);
userData.SetTeamProject(pair.Value);

Config.BotState.SetUserData(ChannelIds.Directline, Config.UserName, userData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public async Task Authorize_A_Valid_LogOn()
const string state = "channel1;user1";

botData.SetProperty("Pin", pin);
botData.SetProperty("Profiles", new List<VstsProfile> { new VstsProfile { Id = Guid.NewGuid() } });

authenticationService
.Setup(a => a.GetToken(code))
Expand All @@ -97,10 +96,10 @@ public async Task Authorize_A_Valid_LogOn()
.Returns(Task.CompletedTask);

var result = await target.Index(code, string.Empty, state) as ViewResult;
var profiles = botData.GetProfiles();
var vstsProfile = botData.GetProperty<VstsProfile>("NotValidatedByPinProfile");

result.Should().NotBeNull();
profiles.Should().NotBeNull().And.HaveCount(2);
vstsProfile.Should().NotBeNull();
((Authorize)result.Model).Pin.Should().Be(pin);
}
}
Expand Down
28 changes: 10 additions & 18 deletions src/Team-Services-Bot.Api.UnitTests/Dialogs/ApprovalsDialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,18 @@ public ApprovalsDialogTests()
{
}

[TestMethod]
public async Task Constructor_Missing_AuthenticationService()
{
Assert.ThrowsException<ArgumentNullException>(() => new ApprovalsDialog(null, null));

await Task.CompletedTask;
}

[TestMethod]
public async Task Constructor_Missing_VstsService()
{
Assert.ThrowsException<ArgumentNullException>(() => new ApprovalsDialog(this.Fixture.AuthenticationService.Object, null));
Assert.ThrowsException<ArgumentNullException>(() => new ApprovalsDialog(null));

await Task.CompletedTask;
}

[TestMethod]
public async Task Constructor()
{
var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, this.Fixture.VstsService.Object);
var target = new ApprovalsDialog(this.Fixture.VstsService.Object);

await Task.CompletedTask;
}
Expand All @@ -62,7 +54,7 @@ public async Task Start()
var toBot = this.Fixture.CreateMessage();
toBot.Text = null;

var mocked = new Mock<ApprovalsDialog>(this.Fixture.AuthenticationService.Object, this.Fixture.VstsService.Object) { CallBase = true };
var mocked = new Mock<ApprovalsDialog>(this.Fixture.VstsService.Object) { CallBase = true };
var target = mocked.Object;

await target.StartAsync(this.Fixture.DialogContext.Object);
Expand Down Expand Up @@ -103,7 +95,7 @@ public async Task List_Approvals()
.Setup(s => s.GetApprovals(account, teamProject, profile))
.ReturnsAsync(approvals);

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, service.Object);
var target = new ApprovalsDialog(service.Object);

await target.ApprovalsAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot));

Expand Down Expand Up @@ -138,7 +130,7 @@ public async Task Approve_Approval_Without_Comment()
.Setup(ud => ud.TryGetValue("TeamProject", out teamProject))
.Returns(true);

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, service.Object)
var target = new ApprovalsDialog(service.Object)
{
Account = account,
Profile = profile,
Expand Down Expand Up @@ -174,7 +166,7 @@ public async Task Approve_Approval()
.Setup(ud => ud.TryGetValue("TeamProject", out teamProject))
.Returns(true);

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, service.Object)
var target = new ApprovalsDialog(service.Object)
{
Account = account,
Profile = profile,
Expand Down Expand Up @@ -211,7 +203,7 @@ public async Task Reject_Approval_Without_Comment()
.Setup(ud => ud.TryGetValue("TeamProject", out teamProject))
.Returns(true);

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, service.Object)
var target = new ApprovalsDialog(service.Object)
{
Account = account,
Profile = profile,
Expand Down Expand Up @@ -245,7 +237,7 @@ public async Task Reject_Approval()
.Setup(ud => ud.TryGetValue("TeamProject", out teamProject))
.Returns(true);

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, this.Fixture.VstsService.Object)
var target = new ApprovalsDialog(this.Fixture.VstsService.Object)
{
Account = account,
Profile = profile,
Expand All @@ -266,7 +258,7 @@ public async Task Approval_Invalid_Message()
var toBot = this.Fixture.CreateMessage();
toBot.Text = "invalid";

var target = new ApprovalsDialog(this.Fixture.AuthenticationService.Object, this.Fixture.VstsService.Object);
var target = new ApprovalsDialog(this.Fixture.VstsService.Object);

await target.ApproveOrRejectAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot));

Expand All @@ -279,7 +271,7 @@ public async Task Change_Status()
var toBot = this.Fixture.CreateMessage();
toBot.Text = "A comment";

var mocked = new Mock<ApprovalsDialog>(this.Fixture.AuthenticationService.Object, this.Fixture.VstsService.Object) { CallBase = true };
var mocked = new Mock<ApprovalsDialog>(this.Fixture.VstsService.Object) { CallBase = true };
var target = mocked.Object;
target.ApprovalId = 1;
target.IsApproved = true;
Expand Down
35 changes: 32 additions & 3 deletions src/Team-Services-Bot.Api.UnitTests/Dialogs/ConnectDialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ namespace Vsar.TSBot.UnitTests
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using Autofac;
using Autofac.Integration.WebApi;
using Cards;
using Common.Tests;
using Dialogs;
using FluentAssertions;
using Microsoft.Bot.Connector;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -161,9 +165,17 @@ public async Task Connect_For_The_Second_Time()
const string appId = "AnAppId";
const string authorizeUrl = "https://www.authorizationUrl.com";

var profile = new VstsProfile();
var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } };
var profiles = new List<VstsProfile> { profile } as IList<VstsProfile>;

var builder = new ContainerBuilder();
builder
.Register((c, x) => this.Fixture.AuthenticationService.Object)
.As<IAuthenticationService>();

var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

var mocked = new Mock<ConnectDialog>(appId, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true };
var target = mocked.Object;

Expand Down Expand Up @@ -193,9 +205,17 @@ public async Task Connect_For_The_Second_Time_With_Account_Selected()
var toBot = this.Fixture.CreateMessage();
toBot.Text = "connect account";

var profile = new VstsProfile();
var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } };
var profiles = new List<VstsProfile> { profile } as IList<VstsProfile>;

var builder = new ContainerBuilder();
builder
.Register((c, x) => this.Fixture.AuthenticationService.Object)
.As<IAuthenticationService>();

var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

var mocked = new Mock<ConnectDialog>(appId, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true };
var target = mocked.Object;

Expand Down Expand Up @@ -225,7 +245,7 @@ public async Task Connect_For_The_Second_Time_With_Account_And_TeamProject_Selec
var toBot = this.Fixture.CreateMessage();
toBot.Text = "connect account teamproject";

var profile = new VstsProfile();
var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } };
IList<VstsProfile> profiles = new List<VstsProfile> { profile };

var mocked = new Mock<ConnectDialog>(appId, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true };
Expand Down Expand Up @@ -276,14 +296,23 @@ public async Task Handle_Received_Pin()
var toBot = this.Fixture.CreateMessage();
toBot.Text = "12345";

var profile = new VstsProfile();
var profiles = new List<VstsProfile>() as IList<VstsProfile>;

var mocked = new Mock<ConnectDialog>(appId, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true };
var target = mocked.Object;
target.Pin = "12345";

this.Fixture.UserData.Setup(ud => ud.TryGetValue("NotValidatedByPinProfile", out profile)).Returns(true);
this.Fixture.UserData.Setup(ud => ud.TryGetValue("Profiles", out profiles)).Returns(true);

mocked.Setup(m => m.ContinueProcess(this.Fixture.DialogContext.Object, toBot)).Returns(Task.CompletedTask).Verifiable();

await target.PinReceivedAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot));

target.Profile.Should().Be(profile);
profiles.Should().Contain(profile);
this.Fixture.UserData.Verify(ud => ud.SetValue("Profile", profile));
mocked.Verify();
}

Expand Down

0 comments on commit ac4bdc8

Please sign in to comment.