private static NotificationInstallationsController InitializeAuthenticatedController() { string signingKey = "6523e58bc0eec42c31b9635d5e0dfc23b6d119b73e633bf3a5284c79bb4a1ede"; // SHA256 hash of 'secret_key' HttpConfiguration config = new HttpConfiguration(); AppServiceTokenHandler handler = new AppServiceTokenHandler(config); string url = "http://localhost"; Claim[] claims = new Claim[] { new Claim("sub", "my:userid") }; // Create a token the same way as App Service Authentication JwtSecurityToken token = AppServiceLoginHandler.CreateToken(claims, signingKey, url, url, TimeSpan.FromDays(10)); // Validate that token and parse it into a ClaimsPrincipal the same way as App Service Authentication ClaimsPrincipal user = null; string[] validIssAud = new[] { url }; handler.TryValidateLoginToken(token.RawData, signingKey, validIssAud, validIssAud, out user); NotificationInstallationsController controller = new NotificationInstallationsController(); controller.Configuration = config; controller.Request = new HttpRequestMessage(); controller.User = user; return(controller); }
public async Task PutInstallations_RegistersUserId_IfAuthenticated() { // Arrange NotificationInstallationsController controller = InitializeAuthenticatedController(); HttpConfiguration config = controller.Configuration; NotificationInstallation notification = GetNotificationInstallation(); // Mock the PushClient and capture the Installation that we send to NH for later verification Installation installation = null; var pushClientMock = new Mock <PushClient>(config); pushClientMock.Setup(p => p.CreateOrUpdateInstallationAsync(It.IsAny <Installation>())) .Returns <Installation>((inst) => { installation = inst; return(Task.FromResult(0)); }); pushClientMock.Setup(p => p.GetRegistrationsByTagAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(Task.FromResult(this.CreateCollectionQueryResult <RegistrationDescription>(Enumerable.Empty <RegistrationDescription>()))); config.SetPushClient(pushClientMock.Object); // Act await controller.PutInstallation(notification.InstallationId, notification); // Assert Assert.NotNull(installation); Assert.Equal(notification.InstallationId, installation.InstallationId); Assert.Equal(notification.PushChannel, installation.PushChannel); Assert.Equal(1, installation.Tags.Count()); Assert.Equal("_UserId:my:userid", installation.Tags[0]); }
public async Task PutInstallations_RegistersUserId_IfAuthenticatedAndTagsExist() { // Arrange NotificationInstallationsController controller = InitializeAuthenticatedController(); HttpConfiguration config = controller.Configuration; NotificationInstallation notification = GetNotificationInstallation(); // Mock the PushClient and capture the Installation that we send to NH for later verification Installation installation = null; var pushClientMock = new Mock <PushClient>(config); pushClientMock.Setup(p => p.CreateOrUpdateInstallationAsync(It.IsAny <Installation>())) .Returns <Installation>((inst) => { installation = inst; return(Task.FromResult(0)); }); pushClientMock.Setup(p => p.GetRegistrationsByTagAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())) .Returns(() => { RegistrationDescription[] registrations = new RegistrationDescription[] { new WindowsRegistrationDescription("http://someuri", new string[] { "tag1", "tag2", "_UserId:something" }) }; return(Task.FromResult(this.CreateCollectionQueryResult <RegistrationDescription>(registrations))); }); config.SetPushClient(pushClientMock.Object); // Act await controller.PutInstallation(notification.InstallationId, notification); // Assert Assert.NotNull(installation); Assert.Equal(notification.InstallationId, installation.InstallationId); Assert.Equal(notification.PushChannel, installation.PushChannel); Assert.Equal(3, installation.Tags.Count()); // verify the existing userid is removed and replaced with current Assert.Equal("_UserId:my:userid", installation.Tags[2]); }