public async Task <IActionResult> PutExperiencePost([FromBody] SaveExperiencePostResource experienceResource, string uuid) { var targetUsername = httpContextRetriever.HttpContext.User.Identity.Name; User user = await userService.GetUserByMailAsync(targetUsername); Metadata metadata = null; try { metadata = await _metadataService.GetByUuidAsync(Guid.Parse(uuid)); } catch (Exception) { throw new HttpException(HttpStatusCode.NotFound); } if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } if (user.UserType < UserType.Municipality || user.MunicipalityName != metadata.MunicipalityName) { return(Unauthorized("The user does not have municipality privilegies for the Metadata given")); } var experience = _mapper.Map <SaveExperiencePostResource, ExperiencePost>(experienceResource); experience.Modified = DateTime.UtcNow; experience.Created = DateTime.UtcNow; experience.LastEditedBy = user; var result = await _experiencePostService.SaveAsync(experience); metadata.ExperiencePosts.Add(new MetadataExperiencePostMapping() { ExperiencePost = experience, Metadata = metadata }); //commit change to metadata await _unitOfWork.CompleteAsync(); var resultSafe = _mapper.Map <SaveExperiencePostResponse, SafeSaveExperiencePostResponse>(result); return(Ok(resultSafe)); }
public async Task PutCommentsAndReplyToCommentTest() { var client = _factory.CreateDefaultClient(); NewUserResource newUserResource = new NewUserResource(); newUserResource.Mail = "*****@*****.**"; newUserResource.Password = "******"; newUserResource.UserType = UserType.Municipality; await client.PutAsync("api/user", ResponseSerializer.Serialize(newUserResource)); AuthUserResource loginResource = new AuthUserResource(); loginResource.Mail = newUserResource.Mail; loginResource.Password = newUserResource.Password; var loginResponse = client.PostAsync("/api/user/auth", ResponseSerializer.Serialize(loginResource)).Result; Assert.NotNull(loginResponse.Content); var user = ResponseSerializer.Extract <PrivateSafeUserResource>(loginResponse); Assert.Equal(loginResource.Mail, user.Mail); string token = user.Token; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var newCategory = new SaveMetadataCategoryResource { ParentUuid = null, Name = "Super Category" }; var newCategoryResponse = await client.PutAsync("api/metadatacategory", ResponseSerializer.Serialize(newCategory)); newCategoryResponse.EnsureSuccessStatusCode(); var category = ResponseSerializer.Extract <MetadataCategory>(newCategoryResponse); SaveMetadataTypeResource newMetadataTypeResource = new SaveMetadataTypeResource { CategoryUuid = category.Uuid, Name = "Super MetadataType" }; var newMetadataTypeResponse = await client.PutAsync("api/metadatatype", ResponseSerializer.Serialize(newMetadataTypeResource)); newMetadataTypeResponse.EnsureSuccessStatusCode(); var metadataType = ResponseSerializer.Extract <MetadataTypeResource>(newMetadataTypeResponse); SaveMetadataResource saveMetadata = new SaveMetadataResource { Description = "Test", MetadataTypeUuid = metadataType.Uuid, MunicipalityName = user.MunicipalityName, ReleaseState = EReleaseState.Green }; var newMetadataResponse = await client.PutAsync("api/metadata/", ResponseSerializer.Serialize(saveMetadata)); newMetadataResponse.EnsureSuccessStatusCode(); var metadata = ResponseSerializer.Extract <MetadataResource>(newMetadataResponse); var newComment = new NewCommentResource { Content = "Well hello there!" }; var putCommentResponse = await client.PutAsync("api/comment/metadata/" + metadata.Uuid, ResponseSerializer.Serialize(newComment)); putCommentResponse.EnsureSuccessStatusCode(); var comment = ResponseSerializer.Extract <CommentResource>(putCommentResponse); Assert.Equal(newComment.Content, comment.Content); Assert.Equal(user.Mail, comment.UserMail); var newReplyComment = new NewCommentResource { Content = "Well, well, well hello there!" }; var replyCommentResponse = await client.PutAsync("api/comment/reply/" + comment.Uuid, ResponseSerializer.Serialize(newReplyComment)); replyCommentResponse.EnsureSuccessStatusCode(); var replyComment = ResponseSerializer.Extract <CommentResource>(replyCommentResponse); Assert.Equal(comment.Uuid, replyComment.ParentCommentUuid); Assert.Equal(newReplyComment.Content, replyComment.Content); Assert.Equal(user.Mail, replyComment.UserMail); var newExperiencePost = new SaveExperiencePostResource { Title = "Big Test", Contents = "Test" }; var newExperiencePostResponse = await client.PutAsync("api/metadata/" + metadata.Uuid + "/experience", ResponseSerializer.Serialize(newExperiencePost)); newExperiencePostResponse.EnsureSuccessStatusCode(); var experiencePost = ResponseSerializer.Extract <ExperiencePostResource>(newExperiencePostResponse); var newCommentExperiencePost = new NewCommentResource { Content = "Well hello there experience!" }; var putCommentExperiencePost = await client.PutAsync("api/comment/experiencepost/" + experiencePost.Uuid, ResponseSerializer.Serialize(newCommentExperiencePost)); putCommentExperiencePost.EnsureSuccessStatusCode(); var commentExperiencePost = ResponseSerializer.Extract <CommentResource>(putCommentExperiencePost); Assert.Equal(newCommentExperiencePost.Content, commentExperiencePost.Content); Assert.Equal(user.Mail, commentExperiencePost.UserMail); }
public async Task Put_NewReleasedMetaData_NewDataSource_GetMetaData_GetMetaDataLike_AttachExperiencePost_EndpointsReturnSuccess_CorrectContentType() { // Arrange var client = _factory.CreateDefaultClient(); NewUserResource newUserResource = new NewUserResource(); newUserResource.Mail = "*****@*****.**"; newUserResource.Password = "******"; newUserResource.UserType = UserType.Municipality; await client.PutAsync("api/user", new StringContent(JsonSerializer.Serialize(newUserResource), Encoding.UTF8, "application/json")); AuthUserResource loginResource = new AuthUserResource(); loginResource.Mail = newUserResource.Mail; loginResource.Password = newUserResource.Password; var loginResponse = await client.PostAsync("/api/user/auth", new StringContent(JsonSerializer.Serialize(loginResource), Encoding.UTF8, "application/json")); Assert.NotNull(loginResponse.Content); var user = ResponseSerializer.Extract <PrivateSafeUserResource>(loginResponse); Assert.Equal(loginResource.Mail, user.Mail); string token = user.Token; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); SaveMetadataResource metaDataResource = new SaveMetadataResource(); metaDataResource.Description = "Test Description"; metaDataResource.MunicipalityName = "Test"; metaDataResource.ReleaseState = EReleaseState.Released; // Act var metaDataResponse = await client.PutAsync("/api/metadata", new StringContent(JsonSerializer.Serialize(metaDataResource), Encoding.UTF8, "application/json")); var metadata = ResponseSerializer.Extract <Metadata>(metaDataResponse); var metaDataGetResponse = await client.GetAsync("/api/metadata/" + metadata.Uuid); var metaDataLikeGetResponse = await client.GetAsync("/api/metadata/" + metadata.Uuid + "/like"); Assert.Equal(user.MunicipalityName, metadata.MunicipalityName); SaveExperiencePostResource experiencePostResource = new SaveExperiencePostResource(); var experienceResponse = await client.PutAsync("/api/metadata/" + metadata.Uuid + "/experience", new StringContent(JsonSerializer.Serialize(experiencePostResource), Encoding.UTF8, "application/json")); NewDataFormatResource dataFormatResource = new NewDataFormatResource(); dataFormatResource.Name = "Name of this dataformat"; dataFormatResource.Description = "Describing this dataformat"; dataFormatResource.DocumentationUrl = "this URL"; dataFormatResource.MimeType = "this MimeType"; var dataFormatResponse = await client.PutAsync("/api/dataformat", new StringContent(JsonSerializer.Serialize(dataFormatResource), Encoding.UTF8, "application/json")); var dataformat = ResponseSerializer.Extract <DataFormat>(dataFormatResponse); NewDataSourceResource dataSourceResource = new NewDataSourceResource(); dataSourceResource.MetadataUuid = metadata.Uuid; dataSourceResource.Url = "This cool url"; dataSourceResource.Description = "Some test source"; dataSourceResource.StartDate = System.DateTime.MinValue; dataSourceResource.EndDate = System.DateTime.MaxValue; dataSourceResource.DataFormatMimeType = dataformat.MimeType; Assert.NotEqual(dataSourceResource.StartDate, dataSourceResource.EndDate); var dataSourceResponse = await client.PutAsync("/api/metadata/url", new StringContent(JsonSerializer.Serialize(dataSourceResource), Encoding.UTF8, "application/json")); // Assert metaDataResponse.EnsureSuccessStatusCode(); // Status Code 200-299 Assert.Equal("application/json; charset=utf-8", metaDataResponse.Content.Headers.ContentType.ToString()); metaDataGetResponse.EnsureSuccessStatusCode(); // Status Code 200-299 Assert.Equal("application/json; charset=utf-8", metaDataGetResponse.Content.Headers.ContentType.ToString()); metaDataLikeGetResponse.EnsureSuccessStatusCode(); // Status Code 200-299 Assert.Equal("application/json; charset=utf-8", metaDataLikeGetResponse.Content.Headers.ContentType.ToString()); experienceResponse.EnsureSuccessStatusCode(); // Status Code 200-299 Assert.Equal("application/json; charset=utf-8", experienceResponse.Content.Headers.ContentType.ToString()); dataSourceResponse.EnsureSuccessStatusCode(); // Status Code 200-299 Assert.Equal("application/json; charset=utf-8", dataSourceResponse.Content.Headers.ContentType.ToString()); }