コード例 #1
0
        public ActiveDirectoryServiceTests()
        {
            _mockRemoteGraphService = new Mock <IRemoteGraphService>();
            _mockLogger             = new Mock <ILogger <ActiveDirectoryService> >();

            var configuration = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfiles()));
            var mapper        = new Mapper(configuration);

            //var cache = new NoCacheService();
            _mockCache = new Mock <ICacheService>();

            _activeDirectoryService = new ActiveDirectoryService(_mockRemoteGraphService.Object,
                                                                 mapper, _mockCache.Object, _mockLogger.Object);

            // Init testdata
            var inMemoryGraphService = new InMemoryGraphService();
            var userId  = Guid.NewGuid().ToString();
            var groupId = Guid.NewGuid().ToString();

            _user      = inMemoryGraphService.GetUserAsync(userId).Result;
            _group     = inMemoryGraphService.GetGroupAsync(groupId).Result;
            _managerId = Guid.NewGuid().ToString();

            var users  = inMemoryGraphService.FindActiveUserAsync(string.Empty).Result;
            var groups = inMemoryGraphService.FindGroupAsync(string.Empty).Result;

            // Init mock behaviour
            _mockRemoteGraphService.Setup(x => x.GetUserAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(_user));

            _mockRemoteGraphService.Setup(x => x.FindActiveUserAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(users));

            _mockRemoteGraphService.Setup(x => x.GetManagerIdOfUserAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(_managerId));

            _mockRemoteGraphService.Setup(x => x.GetGroupAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(_group));

            _mockRemoteGraphService.Setup(x => x.FindGroupAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(groups));
        }
コード例 #2
0
        public RemoteGraphServiceTests()
        {
            _logger             = new Mock <ILogger <RemoteMicrosoftGraphService> >();
            _graphServiceClient = new Mock <IGraphServiceClient>();
            _remoteGraphService = new RemoteMicrosoftGraphService(_graphServiceClient.Object, _logger.Object);
            var inMemoryGraphService = new InMemoryGraphService();

            _user   = inMemoryGraphService.GetUserAsync(Guid.NewGuid().ToString()).Result;
            _users  = inMemoryGraphService.FindActiveUserAsync(string.Empty).Result;
            _group  = inMemoryGraphService.GetGroupAsync(Guid.NewGuid().ToString()).Result;
            _groups = inMemoryGraphService.FindGroupAsync(string.Empty).Result;

            _managerId = Guid.NewGuid().ToString();

            _mockBatchRequest = new Mock <IBatchRequest>();
            var httpResponseMessage = new HttpResponseMessage();

            httpResponseMessage.StatusCode   = HttpStatusCode.NotFound;
            httpResponseMessage.ReasonPhrase = "Not Found";
            var batchResponseContent = new BatchResponseContent(httpResponseMessage);

            _mockBatchRequest.Setup(x => x.PostAsync(It.IsAny <BatchRequestContent>()))
            .ReturnsAsync(batchResponseContent);

            _mockBatchRequestBuilder = new Mock <IBatchRequestBuilder>();
            _mockBatchRequestBuilder.Setup(x => x.Request()).Returns(_mockBatchRequest.Object);

            _mockBaseClient = new Mock <IBaseClient>();
            _mockBaseClient.Setup(x => x.Batch).Returns(_mockBatchRequestBuilder.Object);

            _mockUserRequest = new Mock <IUserRequest>();
            _mockUserRequest.Setup(x => x.Select(It.IsAny <Expression <Func <User, object> > >())).Returns(_mockUserRequest.Object);
            _mockUserRequest.Setup(x => x.GetAsync()).ReturnsAsync(_user);
            _mockUserRequest.Setup(x => x.GetHttpRequestMessage()).Returns(new HttpRequestMessage(new HttpMethod("GET"), new Uri("http://localhost/")));

            _mockReferenceRequest = new Mock <IDirectoryObjectWithReferenceRequest>();
            _mockReferenceRequest.Setup(x => x.Select(It.IsAny <Expression <Func <DirectoryObject, object> > >())).Returns(_mockReferenceRequest.Object);
            _mockReferenceRequest.Setup(x => x.GetAsync()).Returns(Task.FromResult(new DirectoryObject()
            {
                Id = _managerId
            }));

            _mockReferenceRequestBuilder = new Mock <IDirectoryObjectWithReferenceRequestBuilder>();
            _mockReferenceRequestBuilder.Setup(x => x.Request()).Returns(_mockReferenceRequest.Object);

            _mockUserRequestBuilder = new Mock <IUserRequestBuilder>();
            _mockUserRequestBuilder.Setup(x => x.Request()).Returns(_mockUserRequest.Object);
            _mockUserRequestBuilder.Setup(x => x.Manager).Returns(_mockReferenceRequestBuilder.Object);

            _mockUsersCollectionRequest = new Mock <IGraphServiceUsersCollectionRequest>();
            _mockUsersCollectionRequest.Setup(x => x.Select(It.IsAny <Expression <Func <User, object> > >())).Returns(_mockUsersCollectionRequest.Object);
            _mockUsersCollectionRequest.Setup(x => x.Filter(It.IsAny <string>())).Returns(_mockUsersCollectionRequest.Object);
            _mockUsersCollectionRequest.Setup(x => x.GetAsync()).Returns(Task.FromResult(GetUsersCollectionPage()));

            _mockUsersCollectionRequestBuilder = new Mock <IGraphServiceUsersCollectionRequestBuilder>();
            _mockUsersCollectionRequestBuilder.Setup(x => x.Request()).Returns(_mockUsersCollectionRequest.Object);

            _mockGroupsCollectionRequest = new Mock <IGraphServiceGroupsCollectionRequest>();
            _mockGroupsCollectionRequest.Setup(x => x.Select(It.IsAny <Expression <Func <Group, object> > >())).Returns(_mockGroupsCollectionRequest.Object);
            _mockGroupsCollectionRequest.Setup(x => x.Filter(It.IsAny <string>())).Returns(_mockGroupsCollectionRequest.Object);
            _mockGroupsCollectionRequest.Setup(x => x.GetAsync()).Returns(Task.FromResult(GetGroupsCollectionPage()));

            _mockGroupsCollectionRequestBuilder = new Mock <IGraphServiceGroupsCollectionRequestBuilder>();
            _mockGroupsCollectionRequestBuilder.Setup(x => x.Request()).Returns(_mockGroupsCollectionRequest.Object);

            _graphServiceClient.Setup(x => x.Users)
            .Returns(_mockUsersCollectionRequestBuilder.Object);

            _graphServiceClient.Setup(x => x.Users[It.IsAny <string>()])
            .Returns(_mockUserRequestBuilder.Object);

            _graphServiceClient.Setup(x => x.Groups)
            .Returns(_mockGroupsCollectionRequestBuilder.Object);

            _graphServiceClient.Setup(x => x.Batch)
            .Returns(_mockBatchRequestBuilder.Object);
        }