Commit 6ae85973 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '501-admin-user-can-create-the-same-cms-articles' into 'master'

Resolve "Admin user can create the same CMS articles;"

Closes #501

See merge request genesys-pgr/genesys-server!523
parents 38e1e303 36fed77e
......@@ -28,6 +28,7 @@ import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.service.AmphibianService.AmphibianException;
import org.genesys2.server.service.AmphibianService.AmphibianNotAvailableException;
import org.genesys2.server.service.CRMException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.ConcurrencyFailureException;
......@@ -144,7 +145,21 @@ public class ApiExceptionHandler {
LOG.warn("Invalid argument {} for {} provided {} {}", e.getName(), e.getParameter(), request.getMethod(), request.getRequestURL());
return new ApiError<>(e);
}
/**
* Handle CRM exceptions.
*
* @param e the e
* @param request the request
* @return the api error
*/
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler({ CRMException.class })
@ResponseBody
public ApiError<Exception> handleCRMExceptions(final Exception e, final HttpServletRequest request) {
LOG.warn("CRM: {} for {} {}", e.getMessage(), request.getMethod(), request.getRequestURL());
return new ApiError<>(e);
}
/**
* Handle invalid api usage.
......
......@@ -546,6 +546,10 @@ public class ContentServiceImpl implements ContentService {
throw new CRMException("Global articles must first be created in primary language=" + getDefaultLocale().getLanguage());
}
if (primaryArticle != null && primaryArticle.getLang().equals(locale.getLanguage())) {
throw new CRMException(String.format("Global article with slug='%s' and language='%s' already exists.", slug, locale.getLanguage()));
}
Article article = new Article();
if (primaryArticle == null) {
article.setTemplate(isTemplate);
......
......@@ -172,6 +172,42 @@ public class CMSControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
/**
* Expect an error when try to create two global articles
* for the same slug and language
*/
@Test
public void failToCreateGlobalArticleTest() throws Exception {
Article article = new Article();
article.setLang("en");
article.setSlug("test-slug");
/*@formatter:off*/
mockMvc
.perform(post(CMSController.CONTROLLER_URL.concat("/create-article"))
.content(objectMapper.writeValueAsString(article))
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.slug", is(article.getSlug())))
.andExpect(jsonPath("$.lang", is(article.getLang())));
/*@formatter:on*/
// must not save, because already exists
/*@formatter:off*/
mockMvc
.perform(post(CMSController.CONTROLLER_URL.concat("/create-article"))
.content(objectMapper.writeValueAsString(article))
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())));
/*@formatter:on*/
}
@Test
public void listArticlesTest() throws Exception {
final ArticleFilter filter = new ArticleFilter();
......
......@@ -58,7 +58,7 @@ public class ContentServiceTest extends AbstractServicesTest {
contentService.createActivityPost("title of activity post", "summary of activity post", "body of activity post");
// create article for test
testArticle = contentService.createGlobalArticle(ContentService.SMTP_EMAIL_VERIFICATION, contentService.getDefaultLocale(), "title of article", null, "<h3>body of article</h3>", false);
testArticle = contentService.createGlobalArticle(ContentService.SMTP_EMAIL_PASSWORD, contentService.getDefaultLocale(), "title of article", null, "<h3>body of article</h3>", false);
}
@Transactional
......@@ -120,7 +120,7 @@ public class ContentServiceTest extends AbstractServicesTest {
public void getGlobalArticleTets() {
LOG.info("Start test-method getGlobalArticleTets");
assertNotNull(contentService.getGlobalArticle(ContentService.SMTP_EMAIL_VERIFICATION, Locale.ENGLISH));
assertNotNull(contentService.getGlobalArticle(ContentService.SMTP_EMAIL_PASSWORD, Locale.ENGLISH));
LOG.info("Test getGlobalArticleTets is passed!");
}
......@@ -141,7 +141,7 @@ public class ContentServiceTest extends AbstractServicesTest {
@Test
public void getArticleBySlugAndLangTest() {
LOG.info("Start test-method getArticleBySlugAndLangTest");
Article article = contentService.getArticleBySlugAndLang(ContentService.SMTP_EMAIL_VERIFICATION, "en");
Article article = contentService.getArticleBySlugAndLang(ContentService.SMTP_EMAIL_PASSWORD, "en");
assertNotNull(article);
LOG.info("Test getArticleBySlugAndLangTest is passed!");
......@@ -178,6 +178,26 @@ public class ContentServiceTest extends AbstractServicesTest {
LOG.info("Test createGlobalArticleTest is passed!");
}
/**
* Expect an error when try to create two global articles
* for the same slug and language
*/
@Test(expected = CRMException.class)
public void failToCreateSecondGlobalArticleTest() throws CRMException {
String slug = "create_global_article";
Locale locale = Locale.ENGLISH;
String title = "Create Global Article";
String body = "<h1>Create Global Article</h1>";
Article globalArticle = contentService.createGlobalArticle(slug, locale, title, null, body, false);
assertNotNull(globalArticle);
assertEquals(2, contentService.listArticles(new PageRequest(0, 6)).getContent().size());
// expect an error
contentService.createGlobalArticle(slug, locale, title, null, body, false);
}
@Test
public void updateGlobalArticleTest() throws CRMException {
LOG.info("Start test-method updateGlobalArticleTest");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment