Example #1
0
        public async Task <AddPackageWriteRightsForUserResponse> GrantPackageWriteRightsToUserAsync(
            string projectId,
            string packageId,
            AddPackageWriteRightsForUserRequest addPackageWriteRightsForUserRequest)
        {
            if (string.IsNullOrWhiteSpace(projectId))
            {
                throw new ArgumentNullException(nameof(projectId), $"{nameof(projectId)} can not be null, empty or whitespace.");
            }

            if (string.IsNullOrWhiteSpace(packageId))
            {
                throw new ArgumentNullException(nameof(packageId), $"{nameof(packageId)} can not be null, empty or whitespace.");
            }

            if (addPackageWriteRightsForUserRequest == null)
            {
                throw new ArgumentNullException(nameof(addPackageWriteRightsForUserRequest), $"{nameof(addPackageWriteRightsForUserRequest)} can not be null.");
            }

            if (string.IsNullOrWhiteSpace(addPackageWriteRightsForUserRequest.EmployeeId))
            {
                throw new ArgumentNullException(nameof(addPackageWriteRightsForUserRequest), $"{nameof(addPackageWriteRightsForUserRequest.EmployeeId)} can not be null, empty or whitespace.");
            }

            var requestRoute = PackageApiEndpoints.AccessRights.GrantWriteRights(projectId, packageId);

            var requestString  = JsonConvert.SerializeObject(addPackageWriteRightsForUserRequest, this.jsonSerializerSettings);
            var requestContent = ApiHelpers.GetStringContent(requestString);

            var response = await this.httpClient.PutAsync(requestRoute, requestContent);

            if (!response.IsSuccessStatusCode)
            {
                throw new FactroApiException(
                          $"Could not grant read rights to employee with id '{addPackageWriteRightsForUserRequest.EmployeeId}' for package with id '{packageId}' of project with id '{projectId}'.",
                          response.RequestMessage.RequestUri.ToString(),
                          response.StatusCode,
                          response.Content == null ? null : await response.Content.ReadAsStringAsync());
            }

            var responseContentString = await response.Content.ReadAsStringAsync();

            var result =
                JsonConvert.DeserializeObject <AddPackageWriteRightsForUserResponse>(
                    responseContentString,
                    this.jsonSerializerSettings);

            return(result);
        }
        public async Task GrantPackageWriteRightsToUserAsync_ValidRequest_ShouldReturnPackageWriteRightsForUserInformation()
        {
            // Arrange
            var existingProject = new GetProjectPayload
            {
                Id = Guid.NewGuid().ToString(),
            };

            var existingPackage = new GetPackagePayload
            {
                Id = Guid.NewGuid().ToString(),
            };

            var existingEmployeeId = Guid.NewGuid().ToString();

            var addPackageWriteRightsForUserRequest = new AddPackageWriteRightsForUserRequest(existingEmployeeId);

            var expectedPackageWriteRight = new AddPackageWriteRightsForUserResponse
            {
                EmployeeId = existingEmployeeId,
            };

            var expectedResponseContent =
                new StringContent(JsonConvert.SerializeObject(expectedPackageWriteRight, this.fixture.JsonSerializerSettings));

            var expectedResponse = new HttpResponseMessage
            {
                Content = expectedResponseContent,
            };

            var packageApi = this.fixture.GetPackageApi(expectedResponse);

            var addPackageWriteRightsForUserResponse = default(AddPackageWriteRightsForUserResponse);

            // Act
            Func <Task> act = async() => addPackageWriteRightsForUserResponse = await packageApi.GrantPackageWriteRightsToUserAsync(existingProject.Id, existingPackage.Id, addPackageWriteRightsForUserRequest);

            // Assert
            await act.Should().NotThrowAsync();

            addPackageWriteRightsForUserResponse.Should().BeEquivalentTo(expectedPackageWriteRight);
        }
        public async Task GrantPackageWriteRightsToUserAsync_InvalidPackageId_ShouldThrowArgumentNullException(string packageId)
        {
            // Arrange
            var existingProject = new GetProjectPayload
            {
                Id = Guid.NewGuid().ToString(),
            };

            var existingEmployeeId = Guid.NewGuid().ToString();

            var addPackageWriteRightsForUserRequest = new AddPackageWriteRightsForUserRequest(existingEmployeeId);

            var packageApi = this.fixture.GetPackageApi();

            // Act
            Func <Task> act = async() => await packageApi.GrantPackageWriteRightsToUserAsync(existingProject.Id, packageId, addPackageWriteRightsForUserRequest);

            // Assert
            await act.Should().ThrowAsync <ArgumentNullException>();
        }
        public async Task GrantPackageWriteRightsToUserAsync_UnsuccessfulRequest_ShouldThrowFactroApiException()
        {
            // Arrange
            var projectId = Guid.NewGuid().ToString();
            var packageId = Guid.NewGuid().ToString();
            var addPackageWriteRightsForUserRequest = new AddPackageWriteRightsForUserRequest(Guid.NewGuid().ToString());

            var expectedResponse = new HttpResponseMessage
            {
                StatusCode     = HttpStatusCode.BadRequest,
                RequestMessage = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://www.mock-web-address.com"),
                },
            };

            var packageApi = this.fixture.GetPackageApi(expectedResponse);

            // Act
            Func <Task> act = async() => await packageApi.GrantPackageWriteRightsToUserAsync(projectId, packageId, addPackageWriteRightsForUserRequest);

            // Assert
            await act.Should().ThrowAsync <FactroApiException>();
        }