コード例 #1
0
        public async Task <IActionResult> PutAsync([FromBody] SaveMetadataResource resource)
        {
            if (!ModelState.IsValid || !Enum.IsDefined(typeof(EReleaseState), resource.ReleaseState))
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var username = httpContextRetriever.HttpContext.User.Identity.Name;
            var user     = await userService.GetUserByMailAsync(username);

            if (user.UserType == UserType.Municipality && user.MunicipalityName == resource.MunicipalityName)
            {
                var metadata = _mapper.Map <SaveMetadataResource, Metadata>(resource);
                var result   = await _metadataService.SaveAsync(metadata);

                if (!result.Success)
                {
                    return(BadRequest(result.Message));
                }

                var res = _mapper.Map <Metadata, MetadataResource>(metadata);

                await _unitOfWork.CompleteAsync();

                return(Ok(res));
            }

            return(Unauthorized("Invalid permissions!"));
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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());
        }