Exemplo n.º 1
0
        public async Task Admin_ClientApplicationsOperationsTest()
        {
            var applicationId      = 11;
            var testApplication    = new Api.Client.Application("description", applicationId, "name");
            var updatedApplication = new Api.Client.Application("update description", applicationId, "name");

            var applicationRepository     = container.Resolve <IApplicationRepository>();
            var applicationRepositoryMock = Mock.Get(applicationRepository);

            applicationRepositoryMock
            .Setup(x => x.Insert(It.IsAny <Application>()))
            .ReturnsAsync(applicationId);

            applicationRepositoryMock
            .Setup(x => x.FindByIds(It.IsAny <IEnumerable <int> >()))
            .Returns <IEnumerable <int> >(x =>
                                          Task.FromResult(x.Select(a => new Application {
                Id = a
            }).ToArray()));

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer",
                                                  TestContainerBuilder.BuildToken(container, 1, TokenClaims.CLAIM_PERMISSIONS_ADMIN));

                var client = new Domain0Client(TEST_URL, http);

                var id = await client.CreateApplicationAsync(testApplication);

                Assert.Equal(id, applicationId);
                applicationRepositoryMock.Verify(x => x.Insert(It.IsAny <Application>()), Times.Once());

                var loadedApplications = await client.LoadApplicationAsync(applicationId);

                Assert.Contains(loadedApplications, a => a.Id == applicationId);
                applicationRepositoryMock.Verify(x => x.FindByIds(It.IsAny <IEnumerable <int> >()), Times.Once());

                await client.UpdateApplicationAsync(updatedApplication);

                applicationRepositoryMock.Verify(x =>
                                                 x.Update(It.Is <Application>(a =>
                                                                              a.Id == updatedApplication.Id &&
                                                                              a.Description == updatedApplication.Description)),
                                                 Times.Once());

                var filteredApplications = await client.LoadApplicationsByFilterAsync(new ApplicationFilter(new List <int> {
                    3
                }));

                Assert.Contains(filteredApplications, a => a.Id == 3);
                applicationRepositoryMock.Verify(x =>
                                                 x.FindByIds(
                                                     It.Is <IEnumerable <int> >(e => e.Contains(3))),
                                                 Times.Once());

                await client.RemoveApplicationAsync(applicationId);

                applicationRepositoryMock.Verify(x =>
                                                 x.Delete(It.Is <int>(deletedId => deletedId == applicationId)),
                                                 Times.Once());
            }
        }