Beispiel #1
0
            public async Task ShouldCacheReturnedUserIds(
                ulong userId,
                Guid remoteId,
                [Frozen, Substitute] IUserIdCache cache,
                [Frozen, Substitute] ILoginProvidersClient client,
                [Target] DefaultUserService service
                )
            {
                var user = new Models.User {
                    Id = userId
                };
                var login = new UserLogin {
                    LoginProvider = "discord", Enabled = true
                };
                var remoteUser = new Identity.Client.User {
                    Id = remoteId, Logins = new List <UserLogin> {
                        login
                    }
                };
                var cancellationToken = new CancellationToken(false);

                cache.ContainsKey(Any <Snowflake>()).Returns(false);
                client.GetUserByLoginProviderKey(Any <string>(), Any <string>(), Any <CancellationToken>()).Returns(remoteUser);

                await service.IsUserRegistered(user, cancellationToken);

                cache.Received().Add(Is(user.Id), Is <UserId>(id => id.Id == remoteId && id.Enabled));
            }
Beispiel #2
0
            public async Task ShouldReturnFalseIfLoginProvidersClientThrows(
                ulong userId,
                Guid remoteId,
                string errorMessage,
                ApiException exception,
                [Frozen, Substitute] IUserIdCache cache,
                [Frozen, Substitute] ILoginProvidersClient client,
                [Target] DefaultUserService service
                )
            {
                var user = new Models.User {
                    Id = userId
                };
                var remoteUser = new Identity.Client.User {
                    Id = remoteId
                };
                var cancellationToken = new CancellationToken(false);

                cache.ContainsKey(Any <Snowflake>()).Returns(false);
                client.GetUserByLoginProviderKey(Any <string>(), Any <string>(), Any <CancellationToken>()).Returns <Identity.Client.User>(x =>
                {
                    throw exception;
                });

                var result = await service.IsUserRegistered(user, cancellationToken);

                result.Should().BeFalse();

                await client.Received().GetUserByLoginProviderKey(Is("discord"), Is(userId.ToString()), Is(cancellationToken));
            }
Beispiel #3
0
            public async Task ShouldReturnTrueIfUserIdNotInCacheButReturnsFromTheApi(
                ulong userId,
                Guid remoteId,
                [Frozen, Substitute] IUserIdCache cache,
                [Frozen, Substitute] ILoginProvidersClient client,
                [Target] DefaultUserService service
                )
            {
                var user = new Models.User {
                    Id = userId
                };
                var login = new UserLogin {
                    LoginProvider = "discord", Enabled = true
                };
                var remoteUser = new Identity.Client.User {
                    Id = remoteId, Logins = new List <UserLogin> {
                        login
                    }
                };
                var cancellationToken = new CancellationToken(false);

                cache.ContainsKey(Any <Snowflake>()).Returns(false);
                client.GetUserByLoginProviderKey(Any <string>(), Any <string>(), Any <CancellationToken>()).Returns(remoteUser);

                var result = await service.IsUserRegistered(user, cancellationToken);

                result.Should().BeTrue();

                await client.Received().GetUserByLoginProviderKey(Is("discord"), Is(userId.ToString()), Is(cancellationToken));
            }
Beispiel #4
0
            public void ShouldClearTheUserIdCache(
                [Frozen, Substitute] IUserIdCache userIdCache,
                [Target] CacheController controller
                )
            {
                controller.ClearAll();

                userIdCache.Received().Clear();
            }
Beispiel #5
0
            public void ShouldRemoveTheUserIdCacheEntryForTheGivenIdentityId(
                Guid identityId,
                [Frozen, Substitute] IUserIdCache userIdCache,
                [Target] CacheController controller
                )
            {
                controller.ClearUserSpecificCache(identityId);

                userIdCache.Received().RemoveByIdentityId(Is(identityId));
            }
 /// <summary>
 /// Initializes a new instance of the <see cref="CacheController" /> class.
 /// </summary>
 /// <param name="userIdCache">Cache to control user IDs.</param>
 /// <param name="cacheUtils">Utilities for working with the identity cache.</param>
 /// <param name="commandsCache">Service for managing the Brighid Commands cache.</param>
 /// <param name="logger">Logger used to log info to some destination(s).</param>
 public CacheController(
     IUserIdCache userIdCache,
     ICacheUtils cacheUtils,
     IBrighidCommandsCache commandsCache,
     ILogger <CacheController> logger
     )
 {
     this.userIdCache   = userIdCache;
     this.cacheUtils    = cacheUtils;
     this.commandsCache = commandsCache;
     this.logger        = logger;
 }
Beispiel #7
0
            public async Task ShouldReturnTrueIfUserIdExistsInCache(
                ulong userId,
                [Frozen, Substitute] IUserIdCache cache,
                [Target] DefaultUserService service
                )
            {
                var user = new Models.User {
                    Id = userId
                };
                var cancellationToken = new CancellationToken(false);

                cache.ContainsKey(Any <Snowflake>()).Returns(true);

                var result = await service.IsUserRegistered(user, cancellationToken);

                result.Should().BeTrue();
                cache.Received().ContainsKey(Is(user.Id));
            }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultUserService" /> class.
 /// </summary>
 /// <param name="userIdCache">Cache for user ID lookups.</param>
 /// <param name="loginProvidersClient">Client used to manage login providers and linked users in Brighid Identity.</param>
 /// <param name="httpClient">Client used to make arbitrary HTTP requests.</param>
 /// <param name="usersClientFactory">Factory for creating Brighid Identity Users clients with.</param>
 /// <param name="tokenHandler">The security token handler used for reading ID Token JWTs.</param>
 /// <param name="adapterOptions">Misc options used across adapter services.</param>
 /// <param name="logger">Logger used to log info to some destination(s).</param>
 public DefaultUserService(
     IUserIdCache userIdCache,
     ILoginProvidersClient loginProvidersClient,
     HttpClient httpClient,
     IUsersClientFactory usersClientFactory,
     JwtSecurityTokenHandler tokenHandler,
     IOptions <AdapterOptions> adapterOptions,
     ILogger <DefaultUserService> logger
     )
 {
     this.userIdCache          = userIdCache;
     this.loginProvidersClient = loginProvidersClient;
     this.usersClientFactory   = usersClientFactory;
     this.tokenHandler         = tokenHandler;
     this.httpClient           = httpClient;
     this.adapterOptions       = adapterOptions.Value;
     this.logger = logger;
 }
Beispiel #9
0
            public async Task ShouldRetrieveUserIdFromCacheIfPresent(
                Models.User user,
                UserId expectedId,
                [Frozen, Substitute] IUserIdCache userIdCache,
                [Frozen, Substitute] ILoginProvidersClient loginProvidersClient,
                [Target] DefaultUserService service,
                CancellationToken cancellationToken
                )
            {
                userIdCache.TryGetValue(Any <Snowflake>(), out Any <UserId>()).Returns(x =>
                {
                    x[1] = expectedId;
                    return(true);
                });

                var result = await service.GetIdentityServiceUserId(user, cancellationToken);

                result.Should().Be(expectedId);
                await loginProvidersClient.DidNotReceive().GetUserByLoginProviderKey(Is("discord"), Is(user.Id.Value.ToString()), Is(cancellationToken));
            }