public async Task CommandTest()
        {
            await using var _ = await WebHost.Serve();

            // Server commands
            var kv = WebServices.GetRequiredService <IKeyValueService <string> >();

            (await kv.Get("")).Should().BeNull();

            await kv.SetCmd(new IKeyValueService <string> .SetCommand("", "1"));

            (await kv.Get("")).Should().Be("1");

            await WebServices.Commander().Call(new IKeyValueService <string> .SetCommand("", "2"));

            (await kv.Get("")).Should().Be("2");

            // Client commands
            var kvc = ClientServices.GetRequiredService <IKeyValueServiceClient <string> >();

            (await kv.Get("")).Should().Be("2");

            await kvc.SetCmd(new IKeyValueService <string> .SetCommand("", "1"));

            await Task.Delay(100); // Remote invalidation takes some time

            (await kvc.Get("")).Should().Be("1");

            await ClientServices.Commander().Call(new IKeyValueService <string> .SetCommand("", "2"));

            await Task.Delay(100); // Remote invalidation takes some time

            (await kvc.Get("")).Should().Be("2");
        }
Exemplo n.º 2
0
        public async Task BasicTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var authServer     = WebServices.GetRequiredService <IServerSideAuthService>();
            var authClient     = ClientServices.GetRequiredService <IAuthService>();
            var sessionFactory = ClientServices.GetRequiredService <ISessionFactory>();
            var sessionA       = sessionFactory.CreateSession();
            var sessionB       = sessionFactory.CreateSession();
            var alice          = new User("Local", "alice", "Alice");
            var bob            = new User("Local", "bob", "Bob");
            var guest          = new User("<guest>");

            var session = sessionA;
            await WebServices.Commander().CallAsync(new SignInCommand(bob, session).MarkServerSide());

            var user = await authServer.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);

            user = await authClient.GetUserAsync(sessionA);

            user.Name.Should().Be(bob.Name);
            user = await authClient.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);

            session = sessionB;
            user    = await authClient.GetUserAsync(session);

            user.Id.Should().Be(sessionB.Id);
            user.Name.Should().Be(User.GuestName);

            session = sessionFactory.CreateSession();
            user    = await authClient.GetUserAsync(session);

            // User.Id should be equal to new AuthSession.Id
            user.Id.Length.Should().BeGreaterThan(8);
            user.Name.Should().Be(User.GuestName);
        }
        public async Task BasicTest1()
        {
            await using var serving = await WebHost.Serve();

            var authServer     = WebServices.GetRequiredService <IServerSideAuthService>();
            var authClient     = ClientServices.GetRequiredService <IAuthService>();
            var authLocal      = Services.GetRequiredService <IServerSideAuthService>();
            var sessionFactory = ClientServices.GetRequiredService <ISessionFactory>();
            var sessionA       = sessionFactory.CreateSession();
            var sessionB       = sessionFactory.CreateSession();
            var bob            = new User("", "Bob").WithIdentity("g:1");

            var session = sessionA;
            await WebServices.Commander().Call(
                new SignInCommand(session, bob).MarkServerSide());

            var user = await authServer.GetUser(session);

            user.Name.Should().Be(bob.Name);
            long.TryParse(user.Id, out var _).Should().BeTrue();
            user.Claims.Count.Should().Be(0);
            bob = user;

            // Trying to edit user name
            var newName = "Bobby";
            await authClient.EditUser(new(session, newName));

            user = await authServer.GetUser(session);

            user.Name.Should().Be(newName);
            bob = bob with {
                Name = newName
            };

            // Checking if the client is able to see the same user & sessions
            user = await authClient.GetUser(sessionA);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();
            user = await authClient.GetUser(session);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();

            // Checking if local service is able to see the same user & sessions
            if (!Options.UseInMemoryAuthService)
            {
                await Delay(0.5);

                user = await authLocal.GetUser(session);

                user.Id.Should().Be(bob.Id);
                user.IsAuthenticated.Should().BeTrue();
            }

            // Checking guest session
            session = sessionB;
            user    = await authClient.GetUser(session);

            user.IsAuthenticated.Should().BeFalse();

            // Checking sign-out
            await WebServices.Commander().Call(new SignOutCommand(sessionA));

            user = await authServer.GetUser(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            await Delay(0.5);

            user = await authClient.GetUser(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            if (!Options.UseInMemoryAuthService)
            {
                user = await authLocal.GetUser(sessionA);

                user.IsAuthenticated.Should().BeFalse();
            }
        }