public async Task EditCategoryFailDueToInvalidIdsInEndpoint(string email, string password, int id, int invalidId, string categoryName) { var token = await this.Login(email, password); this.client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); var category = new CategoryInputEditModel { Id = id, Name = categoryName }; var json = new StringContent( JsonConvert.SerializeObject(category), Encoding.UTF8, "application/json"); var endpoint = CategoryEditEndpoint + invalidId; var response = await this.client.PutAsync(endpoint, json); var content = JsonConvert.DeserializeObject <ReturnMessage>(await response.Content.ReadAsStringAsync()); Assert.Equal("Invalid ids", content.Message); Assert.Equal(StatusCodes.Status400BadRequest, content.Status); }
public async Task EditCategoryFailDueToUnauthorizedToken(string email, string password, string username, int id, string categoryName) { await this.Register(email, password, username); var token = await this.Login(email, password); this.client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); var category = new CategoryInputEditModel { Id = id, Name = categoryName }; var json = new StringContent( JsonConvert.SerializeObject(category), Encoding.UTF8, "application/json"); var response = await this.client.PutAsync(CategoryEditEndpoint + id, json); var content = JsonConvert.DeserializeObject <ReturnMessage>(await response.Content.ReadAsStringAsync()); Assert.Equal(StatusCodes.Status401Unauthorized, content.Status); Assert.Equal(UnauthorizedError, content.Message); }
public async Task EditCategorySuccessfully(string email, string password, int id, string categoryName) { var token = await this.Login(email, password); this.client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); var category = new CategoryInputEditModel { Id = id, Name = categoryName }; var json = new StringContent( JsonConvert.SerializeObject(category), Encoding.UTF8, "application/json"); var response = await this.client.PutAsync(CategoryEditEndpoint + id, json); response.EnsureSuccessStatusCode(); var content = JsonConvert.DeserializeObject <CreateEditReturnMessage <CategoryViewModel> >(await response.Content.ReadAsStringAsync()); Assert.Equal("Category edited successfully", content.Message); Assert.Equal(StatusCodes.Status200OK, content.Status); }
public async Task <object> Edit(int id, [FromBody] CategoryInputEditModel model) { if (id != model.Id) { return(this.BadRequest(new ReturnMessage { Message = "Invalid ids" })); } if (this.User.IsInRole("Admin")) { try { var category = await this.categoryService.Edit(model); return(this.Ok(new CreateEditReturnMessage <CategoryViewModel> { Message = "Category edited successfully", Data = category })); } catch (Exception e) { return(this.BadRequest(new ReturnMessage { Message = e.Message })); } } return(this.Unauthorized(new ReturnMessage { Message = "You are unauthorized!" })); }
public async Task EditCategoryFailDueToUnauthorized(int id, string categoryName) { var category = new CategoryInputEditModel { Id = id, Name = categoryName }; var json = new StringContent( JsonConvert.SerializeObject(category), Encoding.UTF8, "application/json"); var response = await this.client.PutAsync(CategoryEditEndpoint + id, json); Assert.Equal(StatusCodes.Status401Unauthorized, (int)response.StatusCode); }
public async Task <CategoryViewModel> Edit(CategoryInputEditModel model) { if (IsValidCategoryId(model.Id)) { throw new Exception($"Category with id {model.Id} does not exist."); } if (!IsValidCategoryName(model.Name)) { throw new Exception($"Category with name '{model.Name}' already exists."); } var category = this.Mapper.Map <Category>(model); this.categoryRepository.Update(category); await this.categoryRepository.SaveChangesAsync(); return(this.Mapper.Map <Category, CategoryViewModel>(category)); }