diff --git a/web/src/main/resources/templates/learn/section.html b/web/src/main/resources/templates/learn/section.html index ab54e55..b2c4cae 100644 --- a/web/src/main/resources/templates/learn/section.html +++ b/web/src/main/resources/templates/learn/section.html @@ -44,7 +44,7 @@
- + - - +
СтатьяДействия
СтатьяДействия
@@ -55,7 +55,7 @@ th:text="${article.title}">— скрыто +
@@ -73,7 +73,7 @@
Статей пока нет.
Статей пока нет.
@@ -118,7 +118,7 @@ - +

Новая статья в разделе

@@ -143,7 +143,9 @@
- + + + diff --git a/web/src/test/kotlin/ru/ysu/lms/web/learn/LearnSectionTemplateTest.kt b/web/src/test/kotlin/ru/ysu/lms/web/learn/LearnSectionTemplateTest.kt new file mode 100644 index 0000000..bbd99b2 --- /dev/null +++ b/web/src/test/kotlin/ru/ysu/lms/web/learn/LearnSectionTemplateTest.kt @@ -0,0 +1,106 @@ +package ru.ysu.lms.web.learn + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.mock.web.MockServletContext +import org.thymeleaf.context.WebContext +import org.thymeleaf.spring6.SpringTemplateEngine +import org.thymeleaf.templatemode.TemplateMode +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver +import org.thymeleaf.web.servlet.JakartaServletWebApplication +import ru.ysu.lms.web.session.SessionUser +import java.util.Locale + +class LearnSectionTemplateTest { + private val engine = + SpringTemplateEngine().apply { + setTemplateResolver( + ClassLoaderTemplateResolver().apply { + prefix = "templates/" + suffix = ".html" + characterEncoding = "UTF-8" + templateMode = TemplateMode.HTML + }, + ) + } + + @Test + fun `student does not see article admin actions`() { + val html = render("STUDENT") + + assertThat(html).contains("Статья 1") + assertThat(html).doesNotContain("Удалить статью") + assertThat(html).doesNotContain("Править статью") + assertThat(html).doesNotContain("Скрыть статью") + assertThat(html).doesNotContain("modal-new-article") + assertThat(html).doesNotContain(">Действия") + } + + @Test + fun `admin sees article actions`() { + val html = render("ADMIN") + + assertThat(html).contains("Статья 1") + assertThat(html).contains("Удалить статью") + assertThat(html).contains("Править статью") + assertThat(html).contains("Скрыть статью") + assertThat(html).contains("modal-new-article") + assertThat(html).contains(">Действия") + } + + private fun render(role: String): String { + val servletContext = MockServletContext() + val application = JakartaServletWebApplication.buildApplication(servletContext) + val exchange = + application.buildExchange( + MockHttpServletRequest(servletContext), + MockHttpServletResponse(), + ) + val context = WebContext(exchange, Locale.forLanguageTag("ru")) + context.setVariable("csrfToken", "csrf") + context.setVariable( + "currentUser", + SessionUser( + id = "user-1", + login = role.lowercase(), + role = role, + name = "Пользователь", + mustChangePassword = false, + ), + ) + context.setVariable( + "chapter", + mapOf( + "slug" to "chapter-1", + "title" to "Глава", + ), + ) + context.setVariable( + "section", + mapOf( + "id" to "section-1", + "slug" to "section-1", + "title" to "Раздел", + "published" to true, + "articles" to + listOf( + mapOf( + "id" to "article-1", + "title" to "Статья 1", + "slug" to "article-1", + "published" to true, + ), + ), + ), + ) + context.setVariable("back", "/learn/chapter-1") + context.setVariable("backParent", "/learn") + context.setVariable( + "sectionChoices", + listOf(mapOf("id" to "section-2", "chapterId" to "chapter-1", "label" to "Глава / Раздел 2")), + ) + return engine.process("learn/section", context) + } +}