private static DeviceScopeController MakeController(string targetEdgeId, IList <ServiceIdentity> resultIdentities, IDictionary <string, string> authChains) { var identitiesCache = new Mock <IDeviceScopeIdentitiesCache>(); var edgeHub = new Mock <IEdgeHub>(); edgeHub.Setup(e => e.GetDeviceScopeIdentitiesCache()) .Returns(identitiesCache.Object); identitiesCache.Setup(c => c.GetDevicesAndModulesInTargetScopeAsync(It.Is <string>(id => id == targetEdgeId))) .ReturnsAsync(resultIdentities); foreach (KeyValuePair <string, string> entry in authChains) { identitiesCache.Setup(c => c.GetAuthChain(It.Is <string>(i => i == entry.Key))) .ReturnsAsync(Option.Some <string>(entry.Value)); } foreach (ServiceIdentity identity in resultIdentities) { identitiesCache.Setup(c => c.GetServiceIdentity(It.Is <string>(id => id == identity.Id))) .ReturnsAsync(Option.Some(identity)); } var authenticator = new Mock <IHttpRequestAuthenticator>(); authenticator.Setup(a => a.AuthenticateAsync(It.IsAny <string>(), It.IsAny <Option <string> >(), It.IsAny <Option <string> >(), It.IsAny <HttpContext>())) .ReturnsAsync(new HttpAuthResult(true, string.Empty)); var controller = new DeviceScopeController(Task.FromResult(edgeHub.Object), Task.FromResult(authenticator.Object)); SetupControllerContext(controller); return(controller); }
public async Task HandleGetDeviceAndModuleOnBehalfOfAsync( string actorDeviceId, string actorModuleId, string targetDeviceId, string targetModuleId, string authChain, string originatorDeviceId, string authChainToTarget, HttpStatusCode expectedStatus) { // Setup var request = new IdentityOnBehalfOfRequest(targetDeviceId, targetModuleId, authChain); var identitiesCache = new Mock <IDeviceScopeIdentitiesCache>(); string targetId = targetDeviceId + (string.IsNullOrWhiteSpace(targetModuleId) ? string.Empty : $"/{targetModuleId}"); identitiesCache.Setup(i => i.RefreshServiceIdentityOnBehalfOf(targetId, originatorDeviceId)).Returns(Task.CompletedTask); var targetServiceIdentity = new ServiceIdentity(targetId, "dummy", Enumerable.Empty <string>(), new ServiceAuthentication(ServiceAuthenticationType.None), ServiceIdentityStatus.Enabled); identitiesCache.Setup(i => i.GetServiceIdentity(targetId)).Returns(Task.FromResult(Option.Some(targetServiceIdentity))); identitiesCache.Setup(i => i.GetAuthChain(targetId)).Returns(Task.FromResult(Option.Some(authChainToTarget))); // Act EdgeHubScopeResult edgeHubScopeResult = await DeviceScopeController.HandleGetDeviceAndModuleOnBehalfOfAsync(actorDeviceId, actorModuleId, request, identitiesCache.Object); // Verity Assert.Equal(expectedStatus, edgeHubScopeResult.Status); }
public void ValidateAuthChainForRequestorTest(string actorDeviceId, string targetDeviceId, string authChain, bool expectedResult) { // Act (bool result, string _) = DeviceScopeController.ValidateAuthChainForRequestor(actorDeviceId, targetDeviceId, Option.Maybe(authChain)); // Verify Assert.Equal(expectedResult, result); }
public async Task HandleDevicesAndModulesInTargetDeviceScopeAsyncTest( string actorDeviceId, string actorModuleId, string authChain, string targetDeviceId, string authChainToTarget, HttpStatusCode expectedStatus) { // Setup var request = new NestedScopeRequest(1, string.Empty, authChain); var identitiesCache = new Mock <IDeviceScopeIdentitiesCache>(); identitiesCache.Setup(i => i.GetAuthChain(targetDeviceId)).Returns(Task.FromResult(Option.Some(authChainToTarget))); identitiesCache.Setup(i => i.GetServiceIdentity(targetDeviceId)).Returns(Task.FromResult(Option.None <ServiceIdentity>())); identitiesCache.Setup(i => i.GetDevicesAndModulesInTargetScopeAsync(targetDeviceId)).Returns(Task.FromResult(new List <ServiceIdentity>() as IList <ServiceIdentity>)); // Act EdgeHubScopeResult result = await DeviceScopeController.HandleDevicesAndModulesInTargetDeviceScopeAsync(actorDeviceId, actorModuleId, request, identitiesCache.Object); // Verify Assert.Equal(expectedStatus, result.Status); }