public async Task Handle_Unknown_Account_Received() { var profile1 = new VstsProfile { Accounts = new List <string> { "Account1", "Account2" } }; var profile2 = new VstsProfile { Accounts = new List <string> { "Account3", "Account4" } }; var profiles = new List <VstsProfile> { profile1, profile2 }; var toBot = this.Fixture.CreateMessage(); toBot.Text = "UnknownAccount"; var mocked = new Mock <ConnectDialog>(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.Object) { CallBase = true }; var target = mocked.Object; target.Profiles = profiles; mocked.Setup(m => m.LogOnAsync(this.Fixture.DialogContext.Object, toBot)).Returns(Task.CompletedTask).Verifiable(); await target.AccountReceivedAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); mocked.Verify(); }
public async Task Select_Project() { var account = "Account1"; var profile = new VstsProfile { Accounts = new List <string> { account }, Token = new OAuthToken() }; var projects = new List <TeamProjectReference> { new TeamProjectReference { Name = "Project1" } }; var toBot = this.Fixture.CreateMessage(); var target = new ConnectDialog(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.Object) { Account = account, Profile = profile }; this.Fixture.VstsService.Setup(s => s.GetProjects(account, profile.Token)).ReturnsAsync(projects).Verifiable(); await target.SelectProjectAsync(this.Fixture.DialogContext.Object, toBot); this.Fixture.VstsService.Verify(); this.Fixture.DialogContext .Verify(c => c.PostAsync( It.Is <IMessageActivity>(a => a.Attachments.First().Content is ProjectsCard), CancellationToken.None)); this.Fixture.DialogContext.Verify(c => c.Wait <IMessageActivity>(target.ProjectReceivedAsync)); target.TeamProjects.Should().Contain("Project1"); }
public async Task Handle_Received_Pin() { const string appId = "AnAppId"; const string appScope = "vso.scopes"; const string authorizeUrl = "https://www.authorizationUrl.com"; 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, appScope, 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(); }
public void GetProfile() { var profile = new VstsProfile { Token = new OAuthToken { CreatedOn = DateTime.UtcNow, ExpiresIn = 3600, RefreshToken = "1111" } }; var authenticationService = new Mock <IAuthenticationService>(); var mocked = new Mock <IBotDataBag>(); var builder = new ContainerBuilder(); builder .Register((c, x) => authenticationService.Object) .As <IAuthenticationService>(); var container = builder.Build(); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); mocked.Setup(m => m.TryGetValue("Profile", out profile)).Returns(true).Verifiable(); var result = mocked.Object.GetProfile(); mocked.Verify(); result.Should().Be(profile); result.Token.Should().Be(profile.Token); }
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>(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.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(); }
public async Task Select_Account() { var profile1 = new VstsProfile { Accounts = new List <string> { "Account1", "Account2" } }; var profile2 = new VstsProfile { Accounts = new List <string> { "Account3", "Account4" } }; var profiles = new List <VstsProfile> { profile1, profile2 }; var toBot = this.Fixture.CreateMessage(); var target = new ConnectDialog(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.Object) { Profiles = profiles }; await target.SelectAccountAsync(this.Fixture.DialogContext.Object, toBot); this.Fixture.DialogContext .Verify(c => c.PostAsync( It.Is <IMessageActivity>(a => a.Attachments.First().Content is AccountsCard), CancellationToken.None)); this.Fixture.DialogContext.Verify(c => c.Wait <IMessageActivity>(target.AccountReceivedAsync)); }
public void SetProfile_BotDataBag() { var profile = new VstsProfile(); var mocked = new Mock <IBotDataBag>(); mocked.Object.SetProfile(profile); mocked.Verify(m => m.SetValue("Profile", profile)); }
public async Task Connect_For_The_Second_Time_With_Account_Selected() { var toBot = this.Fixture.CreateMessage(); toBot.Text = "connect account"; var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } }; var profiles = new List <VstsProfile> { profile } as IList <VstsProfile>; Assert.IsNotNull(profiles); var builder = new ContainerBuilder(); var container = builder.Build(); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); var mocked = new Mock <ConnectDialog>(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.Object) { CallBase = true }; var target = mocked.Object; this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profile", out profile)) .Returns(true); this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profiles", out profiles)) .Returns(true); mocked .Setup(m => m.SelectProjectAsync(this.Fixture.DialogContext.Object, toBot)) .Returns(Task.CompletedTask) .Verifiable(); var applicationMock = new Mock <IVstsApplication>(); applicationMock .Setup(application => application.AuthenticationService) .Returns(new Mock <IAuthenticationService>().Object); this.Fixture.VstsApplicationRegistry .Setup(registry => registry.GetVstsApplicationRegistration(It.IsAny <string>())) .Returns(applicationMock.Object); await target.ConnectAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); mocked.Verify(); }
public void SetProfile_BotData() { var profile = new VstsProfile(); var data = new BotData(); data.SetProfile(profile); var result = data.GetProperty <VstsProfile>("Profile"); result.Should().BeOfType <VstsProfile>(); }
public async Task Connect_For_The_Second_Time_With_Account_Selected() { const string appId = "AnAppId"; const string appScope = "vso.scopes"; const string authorizeUrl = "https://www.authorizationUrl.com"; var toBot = this.Fixture.CreateMessage(); toBot.Text = "connect account"; 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, appScope, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true }; var target = mocked.Object; this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profile", out profile)) .Returns(true); this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profiles", out profiles)) .Returns(true); mocked .Setup(m => m.SelectProjectAsync(this.Fixture.DialogContext.Object, toBot)) .Returns(Task.CompletedTask) .Verifiable(); await target.ConnectAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); mocked.Verify(); }
public void GetNotValidatedByPinProfile_BotDataBag() { VstsProfile profile = new VstsProfile(); var mocked = new Mock <IBotDataBag>(); mocked.Setup(m => m.TryGetValue("NotValidatedByPinProfile", out profile)).Returns(true).Verifiable(); var result = mocked.Object.GetNotValidatedByPinProfile(); mocked.Verify(); result.Should().Be(profile); }
public async Task Connect_For_The_Second_Time_With_Account_And_TeamProject_Selected() { var toBot = this.Fixture.CreateMessage(); toBot.Text = "connect account teamproject"; var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } }; IList <VstsProfile> profiles = new List <VstsProfile> { profile }; Assert.IsNotNull(profiles); var mocked = new Mock <ConnectDialog>(this.Fixture.VstsService.Object, this.Fixture.VstsApplicationRegistry.Object) { CallBase = true }; var target = mocked.Object; this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profile", out profile)) .Returns(true); this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profiles", out profiles)) .Returns(true); var applicationMock = new Mock <IVstsApplication>(); applicationMock .Setup(application => application.AuthenticationService) .Returns(new Mock <IAuthenticationService>().Object); this.Fixture.VstsApplicationRegistry .Setup(registry => registry.GetVstsApplicationRegistration(It.IsAny <string>())) .Returns(applicationMock.Object); await target.ConnectAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); this.Fixture.DialogContext .Verify(c => c.PostAsync( It.Is <IMessageActivity>(a => a.Text.Equals($"Dear {toBot.From.Name}. I have connected you to your team project 'teamproject', which is in account 'account'.", StringComparison.OrdinalIgnoreCase)), CancellationToken.None)); this.Fixture.DialogContext .Verify(c => c.Done(It.IsAny <IMessageActivity>())); mocked.Verify(); }
public async Task Connect_For_The_Second_Time_With_Account_And_TeamProject_Selected() { const string appId = "AnAppId"; const string appScope = "vso.scopes"; const string authorizeUrl = "https://www.authorizationUrl.com"; var toBot = this.Fixture.CreateMessage(); toBot.Text = "connect account teamproject"; var profile = new VstsProfile { Token = new OAuthToken { ExpiresIn = 3600 } }; IList <VstsProfile> profiles = new List <VstsProfile> { profile }; var mocked = new Mock <ConnectDialog>(appId, appScope, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true }; var target = mocked.Object; this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profile", out profile)) .Returns(true); this.Fixture.UserData .Setup(ud => ud.TryGetValue("Profiles", out profiles)) .Returns(true); await target.ConnectAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); this.Fixture.DialogContext .Verify(c => c.PostAsync( It.Is <IMessageActivity>(a => a.Text.Equals($"Dear {toBot.From.Name}. I have connected you to your team project 'teamproject', which is in account 'account'.", StringComparison.OrdinalIgnoreCase)), CancellationToken.None)); this.Fixture.DialogContext .Verify(c => c.Done(It.IsAny <IMessageActivity>())); mocked.Verify(); }
public void GivenIsAuthorized() { var authService = new AuthenticationService(Config.AppSecret, Config.AuthorizeUrl); var vstsService = new VstsService(); var data = Config.BotState.GetUserData(ChannelIds.Directline, Config.UserName); var profile = data.GetProperty <VstsProfile>("Profile"); var refreshToken = Config.RefreshToken; if (profile != null && !Config.RefreshTokenReinitialize) { refreshToken = profile.Token.RefreshToken; } Config.RefreshTokenReinitialize = false; var token = authService.GetToken(new OAuthToken { RefreshToken = refreshToken }).Result; var p = vstsService.GetProfile(token).Result; var accounts = vstsService.GetAccounts(token, p.Id).Result; profile = new VstsProfile { Accounts = accounts.Select(a => a.AccountName).ToList(), Id = p.Id, DisplayName = p.DisplayName, EmailAddress = p.EmailAddress, Token = token }; data.SetProfile(profile); data.SetProfiles(new List <VstsProfile> { profile }); Config.BotState.SetUserDataAsync(ChannelIds.Directline, Config.UserName, data).Wait(); Config.Profile = profile; Config.Token = token; }
public async Task Handle_Account_Received() { const string appId = "AnAppId"; const string appScope = "vso.scopes"; const string authorizeUrl = "https://www.authorizationUrl.com"; var profile1 = new VstsProfile { Accounts = new List <string> { "Account1", "Account2" } }; var profile2 = new VstsProfile { Accounts = new List <string> { "Account3", "Account4" } }; var profiles = new List <VstsProfile> { profile1, profile2 }; var toBot = this.Fixture.CreateMessage(); toBot.Text = "Account3"; var mocked = new Mock <ConnectDialog>(appId, appScope, new Uri(authorizeUrl), this.Fixture.VstsService.Object) { CallBase = true }; var target = mocked.Object; target.Profiles = profiles; mocked.Setup(m => m.ContinueProcess(this.Fixture.DialogContext.Object, toBot)).Returns(Task.CompletedTask).Verifiable(); await target.AccountReceivedAsync(this.Fixture.DialogContext.Object, this.Fixture.MakeAwaitable(toBot)); this.Fixture.UserData.Verify(ud => ud.SetValue("Account", "Account3")); this.Fixture.UserData.Verify(ud => ud.SetValue("Profile", profile2)); mocked.Verify(); }
public async Task ChangeApprovalStatusTest() { var account = "MyAccount"; var project = "MyProject"; const int id = 1234; var comment = "My comment"; var status = ApprovalStatus.Undefined; var profile = new VstsProfile { Id = Guid.NewGuid(), Token = this.token, EmailAddress = "*****@*****.**" }; var service = new VstsService(); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.ChangeApprovalStatus(null, project, profile, id, status, comment)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.ChangeApprovalStatus(account, null, profile, id, status, comment)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.ChangeApprovalStatus(account, project, null, id, status, comment)); using (ShimsContext.Create()) { ReleaseApproval updatedApproval = null; InitializeConnectionShim(new VssHttpClientBase[] { GetAccountHttpClient(new List <Account> { new Account(Guid.Empty) { AccountName = "myaccount", AccountUri = new Uri("https://myaccount.visualstudio.com") } }), GetProfileHttpClient(new Profile()), new ShimReleaseHttpClientBase(new ShimReleaseHttpClient2()) { GetApprovalAsyncStringInt32NullableOfBooleanObjectCancellationToken = (p, i, includeHistory, userState, cancellationToken) => Task.Run( () => new ReleaseApproval { Id = i }, cancellationToken), UpdateReleaseApprovalAsyncReleaseApprovalStringInt32ObjectCancellationToken = (releaseApproval, p, i, userState, cancellationToken) => Task.Run( delegate { Assert.AreEqual(project, p); updatedApproval = releaseApproval; return(updatedApproval); }, cancellationToken) }.Instance }); await service.ChangeApprovalStatus(account, project, profile, 4, ApprovalStatus.Canceled, comment); Assert.IsNotNull(updatedApproval); Assert.AreEqual(4, updatedApproval.Id); Assert.AreEqual(ApprovalStatus.Canceled, updatedApproval.Status); Assert.AreEqual(comment, updatedApproval.Comments); } }
public async Task GetApprovalsTest() { var accountName = "MyAccount"; var projectName = "MyProject"; var profile = new VstsProfile { Id = Guid.NewGuid(), Token = this.token, EmailAddress = "*****@*****.**" }; var service = new VstsService(); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.GetApprovals(null, projectName, profile)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.GetApprovals(accountName, null, profile)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await service.GetApprovals(accountName, projectName, null)); using (ShimsContext.Create()) { var expected = new List <ReleaseApproval> { new ReleaseApproval { Id = 1234, ApprovalType = ApprovalType.Undefined } }; InitializeConnectionShim(new VssHttpClientBase[] { GetAccountHttpClient(new List <Account> { new Account(Guid.Empty) { AccountName = "myaccount", AccountUri = new Uri("https://myaccount.visualstudio.com") } }), GetProfileHttpClient(new Profile()), new ShimReleaseHttpClient2 { GetApprovalsAsync2StringStringNullableOfApprovalStatusIEnumerableOfInt32NullableOfApprovalTypeNullableOfInt32NullableOfInt32NullableOfReleaseQueryOrderNullableOfBooleanObjectCancellationToken = (project, assignedToFilter, statusFilter, releaseIdsFilter, typeFilter, top, continuationToken, queryOrder, includeMyGroupApprovals, userState, cancellationToken) => Task.Run( () => (IPagedCollection <ReleaseApproval>) new StubIPagedCollection <ReleaseApproval> { CountGet = () => expected.Count, ItemGetInt32 = i => expected[i] }, cancellationToken) }.Instance }); IList <ReleaseApproval> actual = await service.GetApprovals(accountName, projectName, profile); Assert.AreEqual(expected.Count, actual.Count); for (var i = 0; i < actual.Count; i++) { Assert.AreEqual(expected[i], actual[i]); } } }