Esempio n. 1
0
        public ActionResult Create()
        {
            var brand  = new CreateModelViewModel();
            var brands = brandsRepo.GetAll();

            ViewBag.Brands = new SelectList(brands, "ID", "Name");
            return(View(brand));
        }
Esempio n. 2
0
        public async Task PostDatamodel_FromFormPost_ShouldReturnCreatedFromTemplate(string relativeDirectory, bool altinn2Compatible, string sourceRepository)
        {
            // Arrange
            var org              = "ttd";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            var client = GetTestClient();
            var url    = $"{_versionPrefix}/{org}/{targetRepository}/Datamodels/Post";

            var createViewModel = new CreateModelViewModel()
            {
                ModelName = "test", RelativeDirectory = relativeDirectory, Altinn2Compatible = altinn2Compatible
            };
            var postRequestMessage = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = JsonContent.Create(createViewModel, null, new System.Text.Json.JsonSerializerOptions()
                {
                    PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase
                })
            };

            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(client, postRequestMessage);

            // Act / Assert
            try
            {
                var postResponse = await client.SendAsync(postRequestMessage);

                Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode);

                Assert.Equal("application/json", postResponse.Content.Headers.ContentType.MediaType);

                var postContent = await postResponse.Content.ReadAsStringAsync();

                Json.Schema.JsonSchema postJsonSchema = Json.Schema.JsonSchema.FromText(postContent);
                Assert.NotNull(postJsonSchema);

                // Try to read back the created schema to verify it's stored
                // at the location provided in the post response
                var location          = postResponse.Headers.Location;
                var getRequestMessage = new HttpRequestMessage(HttpMethod.Get, location);
                var getResponse       = await client.SendAsync(getRequestMessage);

                var getContent = await getResponse.Content.ReadAsStringAsync();

                var getJsonSchema = Json.Schema.JsonSchema.FromText(getContent);
                Assert.NotNull(getJsonSchema);
                Assert.Equal(postContent, getContent);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Esempio n. 3
0
        public ActionResult Create(CreateModelViewModel modelView)
        {
            var model = new DataAccess.Model();

            model.Name  = modelView.Name;
            model.Brand = modelView.Brand;
            repo.Create(model);

            return(RedirectToAction("Index"));
        }
Esempio n. 4
0
        public async Task PostDatamodel_InvalidFormPost_ShouldReturnBadRequest(string modelName, string relativeDirectory, bool altinn2Compatible)
        {
            var client = GetTestClient();
            var url    = $"{_versionPrefix}/xyz/dummyRepo/Datamodels/Post";

            var createViewModel = new CreateModelViewModel()
            {
                ModelName = modelName, RelativeDirectory = relativeDirectory, Altinn2Compatible = altinn2Compatible
            };
            var postRequestMessage = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = JsonContent.Create(createViewModel, null, new System.Text.Json.JsonSerializerOptions()
                {
                    PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase
                })
            };

            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(client, postRequestMessage);

            var postResponse = await client.SendAsync(postRequestMessage);

            Assert.Equal(HttpStatusCode.BadRequest, postResponse.StatusCode);
        }
Esempio n. 5
0
        public async Task <ActionResult <string> > Post(string org, string repository, [FromBody] CreateModelViewModel createModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);

            var(relativePath, model) = await _schemaModelService.CreateSchemaFromTemplate(org, repository, developer, createModel.ModelName, createModel.RelativeDirectory, createModel.Altinn2Compatible);

            // Sets the location header and content-type manually instead of using CreatedAtAction
            // because the latter overrides the content type and sets it to text/plain.
            var baseUrl     = GetBaseUrl();
            var locationUrl = $"{baseUrl}/designer/api/{org}/{repository}/datamodels/{relativePath}";

            Response.Headers.Add("Location", locationUrl);
            Response.StatusCode = (int)HttpStatusCode.Created;

            return(Content(model, "application/json"));
        }