public static AuthenticationFilter GetTarget( Mock <ITokensManager> tokensManagerMock = null, Mock <IMemoryCacheFacade> memoryCacheFacedeMock = null, Mock <ITenantObjectsManager> tenantObjectsManagerMock = null) { tokensManagerMock ??= TokensManagerTestsHelpers.GetTokensManagerMock(); memoryCacheFacedeMock ??= MemoryCacheFacadeTestHelpers.GetMemoryCacheFacedeMock(); tenantObjectsManagerMock ??= TenantObjectsManagerTestHelpers.GetTenantObjectsManagerMock(); var authFilter = new AuthenticationFilter(tokensManagerMock.Object, memoryCacheFacedeMock.Object, tenantObjectsManagerMock.Object); return(authFilter); }
public async Task TokenSetIntoCache_IfRequestOwner_RetrievedFromDataStorage() { var tokensManager = TokensManagerTestsHelpers.GetTokensManagerMock(); var tokenDetails = new Tokens { OwnerId = 1, MnemoxAccessObjectsType = MnemoxAccessObjectsTypesEnum.USER, }; tokensManager.Setup(x => x.GetTokenDetailsFromDataStorgeAsync(It.IsAny <string>())).ReturnsAsync(tokenDetails); double tokenTtlMinutes = 60; tokensManager.Setup(x => x.GetTokenTtlMinutes(It.IsAny <DateTime>())).Returns(tokenTtlMinutes); var tenantsManager = TenantObjectsManagerTestHelpers.GetTenantObjectsManagerMock(); var tenants = new List <Tenant>(); tenantsManager.Setup(x => x.GetObjectTenantsAsync( It.IsAny <long>(), It.IsAny <MnemoxAccessObjectsTypesEnum>())). ReturnsAsync(tenants); var cacheFacade = MemoryCacheFacadeTestHelpers.GetMemoryCacheFacedeMock(); var target = AuthenticationFilterTestsHelpers.GetTarget( tokensManagerMock: tokensManager, tenantObjectsManagerMock: tenantsManager, memoryCacheFacedeMock: cacheFacade); var context = AuthenticationFilterTestsHelpers.GetActionExecutingContext(); var metadata = new List <IFilterMetadata>(); ActionExecutionDelegate next = () => { var ctx = new ActionExecutedContext(context, metadata, null); return(Task.FromResult(ctx)); }; await target.OnActionExecutionAsync(context, next); cacheFacade.Verify(x => x.Set(It.IsAny <string>(), It.IsAny <RequestOwner>(), It.IsAny <TimeSpan>()), Times.Once); }
public async Task ReturnsUnauthorizedRequest_IfRequestOwner_HasNoTenantsRelation() { var tokensManager = TokensManagerTestsHelpers.GetTokensManagerMock(); var tokenDetails = new Tokens(); tokensManager.Setup(x => x.GetTokenDetailsFromDataStorgeAsync(It.IsAny <string>())).ReturnsAsync(tokenDetails); var tenantsManager = TenantObjectsManagerTestHelpers.GetTenantObjectsManagerMock(); var target = AuthenticationFilterTestsHelpers.GetTarget( tokensManagerMock: tokensManager, tenantObjectsManagerMock: tenantsManager); var context = AuthenticationFilterTestsHelpers.GetActionExecutingContext(); await target.OnActionExecutionAsync(context, null); Assert.IsType <UnauthorizedObjectResult>(context.Result); }