public async void ClientDetach() { var testContext = TestContext.MockUp(); var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object); var attachedTokenStoreMock = new Mock <ITestStore>(); domain0Context.AttachTokenStore(attachedTokenStoreMock.Object); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); attachedTokenStoreMock .VerifySet(env => env.Token = It.Is <string>(s => string.IsNullOrWhiteSpace(s)), Times.Once); domain0Context.DetachTokenStore(attachedTokenStoreMock.Object); profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); attachedTokenStoreMock .VerifySet(env => env.Token = It.Is <string>(s => string.IsNullOrWhiteSpace(s)), Times.Once); }
public async void Refresh() { var testContext = TestContext.MockUp(accessValidTime: 0.1); var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object, enableAutoRefreshTimer: true, reserveTimeToUpdateToken: 0); Assert.False(domain0Context.IsLoggedIn); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); await Task.Delay(1000); testContext.ClientScopeMock .VerifySet(callTo => callTo.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.AtLeast(5)); Assert.True(domain0Context.IsLoggedIn); domain0Context.Dispose(); }
public async void ClientShouldLoginWhenAttached() { var testContext = TestContext.MockUp(); var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object); var attachedClientMock = new Mock <ITestClient>(); var attachedClient = attachedClientMock.Object; var attachedEnvironmentMock = new Mock <IClientScope <ITestClient> >(); attachedEnvironmentMock .Setup(callTo => callTo.Client) .Returns(() => attachedClient); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); var proxy = domain0Context.AttachClientEnvironment(attachedEnvironmentMock.Object); Assert.NotNull(proxy); attachedEnvironmentMock .VerifySet(env => env.Token = It.IsAny <string>(), Times.Once); }
public async void AttachedClientRefresh() { var testContext = TestContext.MockUp(accessValidTime: 0.1); var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object, enableAutoRefreshTimer: true, reserveTimeToUpdateToken: 0); var attachedClientMock = new Mock <ITestClient>(); var attachedClient = attachedClientMock.Object; var attachedEnvironmentMock = new Mock <IClientScope <ITestClient> >(); attachedEnvironmentMock .Setup(callTo => callTo.Client) .Returns(() => attachedClient); var proxy = domain0Context.AttachClientEnvironment(attachedEnvironmentMock.Object); Assert.NotNull(proxy); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); await domain0Context.LoginByPhone(123, "2"); await domain0Context.LoginByPhone(123, "2"); await domain0Context.LoginByPhone(123, "2"); await domain0Context.LoginByPhone(123, "2"); await Task.Delay(1000); attachedEnvironmentMock .VerifySet(env => env.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.AtLeast(5)); }
public async void AttachedClientRefresh() { var testContext = TestContext.MockUp(accessValidTime: 0.1); var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object); var attachedClientMock = new Mock <ITestClient>(); var attachedClient = attachedClientMock.Object; var attachedEnvironmentMock = new Mock <ClientLockScope <ITestClient> >(); attachedEnvironmentMock .Setup(callTo => callTo.Client) .Returns(() => attachedClient); var proxy = domain0Context.AttachClientEnvironment(attachedEnvironmentMock.Object); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); proxy.MethodB(); attachedEnvironmentMock .VerifySet(env => env.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.Exactly(2)); attachedClientMock .Verify(c => c.MethodB(), Times.Once); await proxy.MethodA(); attachedEnvironmentMock .VerifySet(env => env.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.Exactly(3)); attachedClientMock .Verify(c => c.MethodA(), Times.Once); }
public async void LoginBySms() { var testContext = TestContext.MockUp(); using (var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object)) { Assert.False(domain0Context.IsLoggedIn); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); testContext.ClientScopeMock .VerifySet(callTo => callTo.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.Once); Assert.True(domain0Context.IsLoggedIn); } }
public async void CheckRights_Graned_Permission() { var testContext = TestContext.MockUp(); using (var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object)) { Assert.False(domain0Context.IsLoggedIn); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); Assert.True(domain0Context.HavePermission("claimsA")); Assert.True(domain0Context.HavePermission("claimsB")); Assert.True(domain0Context.HavePermissions(new[] { "claimsA", "claimsB" })); Assert.True(domain0Context.HavePermissions(new[] { "claimsA" })); } }
public async void ShouldRemember() { AccessTokenResponse savedLoginInfo = null; var testContext = TestContext.MockUp(accessValidTime: 0.1); testContext.LoginInfoStorageMock .Setup(callTo => callTo .Save(It.IsAny <AccessTokenResponse>())) .Callback <AccessTokenResponse>(x => { savedLoginInfo = x; }); testContext.LoginInfoStorageMock .Setup(callTo => callTo.Load()) .Returns(() => savedLoginInfo); using (var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object)) { Assert.False(domain0Context.IsLoggedIn); domain0Context.ShouldRemember = true; var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); Assert.True(domain0Context.IsLoggedIn); using (var newDomain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, externalStorage: testContext.LoginInfoStorageMock.Object)) { Assert.True(newDomain0Context.IsLoggedIn); } } }
public async void AutoRefresh() { var testContext = TestContext.MockUp(accessValidTime: 0.1); using (var domain0Context = new AuthenticationContext( domain0ClientEnvironment: testContext.ClientScopeMock.Object, reserveTimeToUpdateToken: 0, externalStorage: testContext.LoginInfoStorageMock.Object)) { Assert.False(domain0Context.IsLoggedIn); var profile = await domain0Context.LoginByPhone(123, "2"); Assert.NotNull(profile); testContext.ClientScopeMock .VerifySet(callTo => callTo.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.Once); Assert.True(domain0Context.IsLoggedIn); await Task.Delay(100); var phone = await domain0Context.Client.PhoneByUserIdAsync(1); Assert.Equal(0, phone); testContext.ClientScopeMock .VerifySet(callTo => callTo.Token = It.Is <string>(s => !string.IsNullOrWhiteSpace(s)), Times.Exactly(2)); Assert.True(domain0Context.IsLoggedIn); } }