private static void SetUserRole(HttpRequestData data, UserRoles role) { var service = data.FunctionContext.InstanceServices.GetRequiredService <IAccessTokenService>(); var info = new AccessTokenUserInfo { IsValid = true, UserRole = role }; service.ValidateTokenAsync(data).Returns(info); }
public async Task SaveUserPropertiesAsyncShouldAllowOnlyAdministrators() { var accessTokenService = Substitute.For <IAccessTokenService>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.User }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService)); var request = MockFunction.GetRequest(null, context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); }
public async Task SavePluginsAsyncShouldClearCache() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var memoryCache = Substitute.For <IMemoryCache>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.Administrator }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService), new ServiceDescriptor(typeof(IMemoryCache), memoryCache)); var request = MockFunction.GetRequest("[]", context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); memoryCache.Received().Remove("plugins"); }
public async Task SavePluginsAsyncShouldSaveInStorage() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var memoryCache = Substitute.For <IMemoryCache>(); var user = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.Administrator }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService), new ServiceDescriptor(typeof(IMemoryCache), memoryCache)); var request = MockFunction.GetRequest( "[{\"id\": \"1\", \"key\":\"a\", \"name\":\"A\",\"type\":\"a1\", \"enabled\": true, \"groups\": null }]", context); accessTokenService.ValidateTokenAsync(request).Returns(user); await Commands.SavePluginsAsync(request, context); storageService.Received().AddOrUpdatePluginsAsync(Arg.Is <IReadOnlyList <Plugin> >(list => list.Count == 1 && list[0].Id == "1")); }
public async Task SaveUserPropertiesAsyncShouldOnlyUpdateGroups() { var accessTokenService = Substitute.For <IAccessTokenService>(); var storageService = Substitute.For <IStorageService>(); var userInfo = new AccessTokenUserInfo { IsValid = true, UserRole = UserRoles.User }; var user = new User { Id = "2", Name = "Original", Properties = null }; var context = MockFunction.GetContext( new ServiceDescriptor(typeof(IAccessTokenService), accessTokenService), new ServiceDescriptor(typeof(IStorageService), storageService)); var request = MockFunction.GetRequest( "[{\"id\":\"2\",\"email\":\"[email protected]\",\"name\":\"Test\",\"properties\":{\"p1\":[[\"key\":\"k1\",\"value\":\"v1\"]]}}]", context); storageService.GetUserByEmailAsync("[email protected]").Returns(user); accessTokenService.ValidateTokenAsync(request).Returns(userInfo); await Commands.SavePluginsAsync(request, context); storageService.Received().AddOrUpdateUserAsync(Arg.Is <User>(u => u.Name == "Original" && u.Properties.Count == 1)); }