yeg
2026-09-14 5364ba7487eb7abe0d4adc67f3393f511ed123a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.talentcard.server;
 
import static org.assertj.core.api.Assertions.assertThat;
 
import java.util.Map;
 
import com.talentcard.server.business.PageResult;
import com.talentcard.server.business.admin.AdminBusinessService;
import com.talentcard.server.business.admin.NewsArticleRequest;
import com.talentcard.server.business.portal.PortalService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.beans.factory.annotation.Autowired;
 
@SpringBootTest
@ActiveProfiles("test")
class TalentCardServerApplicationTests {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Autowired
    private PortalService portalService;
 
    @Autowired
    private AdminBusinessService adminBusinessService;
 
    @BeforeEach
    void clearNewsArticles() {
        jdbcTemplate.update("delete from news_article");
    }
 
    @Test
    void contextLoads() {
    }
 
    @Test
    void publishedNewsIsListedAndDetailIncrementsViewCount() {
        jdbcTemplate.update("""
                insert into news_article(title, summary, source, content, view_count, is_top, sort_no, publish_time, status)
                values (?, ?, ?, ?, ?, ?, ?, current_timestamp, ?)
                """, "已发布资讯", "摘要", "东区发布", "<p>正文</p>", 7, 1, 10, 1);
        jdbcTemplate.update("""
                insert into news_article(title, summary, source, content, view_count, is_top, sort_no, publish_time, status)
                values (?, ?, ?, ?, ?, ?, ?, current_timestamp, ?)
                """, "草稿资讯", "草稿摘要", "东区发布", "<p>草稿</p>", 0, 0, 0, 0);
 
        PageResult<Map<String, Object>> page = portalService.news(1, 10);
 
        assertThat(page.total()).isEqualTo(1);
        assertThat(page.records()).hasSize(1);
        Long id = ((Number) page.records().get(0).get("id")).longValue();
        Map<String, Object> detail = portalService.newsDetail(id);
 
        assertThat(detail.get("title")).isEqualTo("已发布资讯");
        assertThat(((Number) detail.get("viewCount")).intValue()).isEqualTo(8);
    }
 
    @Test
    void adminCanPublishNewsForThePortalAndContentIsSanitized() {
        Map<String, Object> draft = adminBusinessService.createNews(new NewsArticleRequest(
                "后台发布资讯", "用于联调", "", "东区发布", "管理员",
                "<p>安全正文</p><script>alert('x')</script>", true, 12, 0), "测试管理员");
        Long id = ((Number) draft.get("id")).longValue();
 
        assertThat(portalService.news(1, 10).total()).isZero();
 
        adminBusinessService.updateNewsStatus(id, (byte) 1);
        PageResult<Map<String, Object>> visible = portalService.news(1, 10);
        Map<String, Object> detail = portalService.newsDetail(id);
 
        assertThat(visible.total()).isEqualTo(1);
        assertThat(detail.get("content").toString()).contains("安全正文").doesNotContain("script");
    }
 
}