2024-04-16 12:38:08 -07:00
|
|
|
package templatetests_test
|
2024-04-01 12:38:49 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
"github.com/gochan-org/gochan/pkg/config"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
_ "github.com/gochan-org/gochan/pkg/gcsql/initsql"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gctemplates"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
|
|
|
|
_ "github.com/gochan-org/gochan/pkg/posting/uploads/inituploads"
|
2024-04-01 17:17:43 -07:00
|
|
|
"github.com/gochan-org/gochan/pkg/server/serverutil"
|
2024-04-01 12:38:49 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
selectBoardsQueryExpectation = `SELECT\s+boards\.id, section_id,\s*uri,\s*dir,\s*navbar_position,\s*title,\s*` +
|
|
|
|
`subtitle,\s*description,\s*max_file_size,\s*max_threads,\s*default_style,\s*locked,\s*created_at,\s*` +
|
|
|
|
`anonymous_name,\s*force_anonymous,\s*autosage_after,\s*no_images_after,\s*max_message_length,\s*` +
|
|
|
|
`min_message_length,\s*allow_embeds,\s*redirect_to_thread,\s*require_file,\s*enable_catalog\s+` +
|
|
|
|
`FROM boards\s+INNER JOIN\s*\(\s*SELECT id,\s*hidden\s+FROM sections\s*\)\s+s\s+ON ` +
|
|
|
|
`boards\.section_id = s\.id\s+WHERE s\.hidden = FALSE ORDER BY navbar_position ASC, boards\.id ASC`
|
|
|
|
|
|
|
|
selectSectionsQueryExpectation = `SELECT\s+id,\s*name,\s*abbreviation,\s*position,\s*hidden\s+FROM\s+sections\s+WHERE\s+hidden\s*=\s*FALSE\s+` +
|
|
|
|
`ORDER BY\s+position ASC,\s*name ASC`
|
|
|
|
)
|
|
|
|
|
2024-04-01 17:17:43 -07:00
|
|
|
func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock, which ...string) bool {
|
2024-04-01 12:38:49 -07:00
|
|
|
t.Helper()
|
2024-04-02 11:33:13 -07:00
|
|
|
_, err := testutil.GoToGochanRoot(t)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-04-01 12:38:49 -07:00
|
|
|
rows := sqlmock.NewRows([]string{"boards.id", "section_id", "uri", "dir", "navbar_position", "title",
|
|
|
|
"subtitle", "description", "max_file_size", "max_threads", "default_style", "locked", "created_at",
|
|
|
|
"anonymous_name", "force_anonymous", "autosage_after", "no_images_after", "max_message_length",
|
|
|
|
"min_message_length", "allow_embeds", "redirect_to_thread", "require_file", "enable_catalog"}).
|
|
|
|
AddRow(1, 1, "test", "test", 1, "Testing board", "Board for testing", "Board for testing", 500, 500,
|
2024-04-10 11:17:26 -07:00
|
|
|
"pipes.css", false, time.Now(), "Anonymous", false, 200, 500, 1500, 0, false, false, false, true).
|
|
|
|
AddRow(2, 1, "test2", "test2", 2, "Testing board #2", "Board for testing", "Board for testing", 500, 500,
|
2024-04-01 12:38:49 -07:00
|
|
|
"pipes.css", false, time.Now(), "Anonymous", false, 200, 500, 1500, 0, false, false, false, true)
|
|
|
|
|
|
|
|
mock.ExpectPrepare(selectBoardsQueryExpectation).
|
|
|
|
ExpectQuery().WithoutArgs().WillReturnRows(rows)
|
|
|
|
|
|
|
|
rows = sqlmock.NewRows([]string{"id", "name", "abbreviation", "position", "hidden"}).
|
|
|
|
AddRow(1, "Main", "main", 1, false)
|
|
|
|
|
|
|
|
mock.ExpectPrepare(selectSectionsQueryExpectation).
|
|
|
|
ExpectQuery().WithoutArgs().WillReturnRows(rows)
|
|
|
|
|
|
|
|
config.SetTestTemplateDir("templates")
|
|
|
|
|
2024-04-02 11:33:13 -07:00
|
|
|
if !assert.NoError(t, gctemplates.InitTemplates(which...)) {
|
2024-04-01 12:38:49 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return assert.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:33:13 -07:00
|
|
|
func runTemplateTestCases(t *testing.T, templateName string, testCases []templateTestCase) {
|
|
|
|
t.Helper()
|
2024-04-01 17:17:43 -07:00
|
|
|
db, mock, err := sqlmock.New()
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2024-10-13 23:18:29 -07:00
|
|
|
config.SetVersion("4.0.0")
|
2024-04-01 17:17:43 -07:00
|
|
|
config.SetTestDBConfig("mysql", "localhost", "gochan", "gochan", "gochan", "")
|
|
|
|
if !assert.NoError(t, gcsql.SetTestingDB("mysql", "gochan", "", db)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !initTemplatesMock(t, mock) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serverutil.InitMinifier()
|
2024-04-02 11:33:13 -07:00
|
|
|
for _, tC := range testCases {
|
2024-04-01 17:17:43 -07:00
|
|
|
t.Run(tC.desc, func(t *testing.T) {
|
2024-04-02 11:33:13 -07:00
|
|
|
tC.Run(t, templateName)
|
2024-04-01 17:17:43 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:33:13 -07:00
|
|
|
func TestBanPageTemplate(t *testing.T) {
|
|
|
|
runTemplateTestCases(t, gctemplates.BanPage, banPageCases)
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:17:26 -07:00
|
|
|
func TestBoardPageTemplate(t *testing.T) {
|
|
|
|
runTemplateTestCases(t, gctemplates.BoardPage, boardPageTestCases)
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:33:13 -07:00
|
|
|
func TestJsConstsTemplate(t *testing.T) {
|
|
|
|
runTemplateTestCases(t, gctemplates.JsConsts, jsConstsCases)
|
|
|
|
}
|
|
|
|
|
2024-04-01 12:38:49 -07:00
|
|
|
func TestTemplateBase(t *testing.T) {
|
|
|
|
db, mock, err := sqlmock.New()
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config.SetTestDBConfig("mysql", "localhost", "gochan", "gochan", "gochan", "")
|
|
|
|
if !assert.NoError(t, gcsql.SetTestingDB("mysql", "gochan", "", db)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
initTemplatesMock(t, mock)
|
|
|
|
}
|
2024-12-31 09:40:01 +01:00
|
|
|
|
|
|
|
func TestBaseFooter(t *testing.T) {
|
|
|
|
runTemplateTestCases(t, gctemplates.PageFooter, baseFooterCases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseHeader(t *testing.T) {
|
|
|
|
runTemplateTestCases(t, gctemplates.PageHeader, baseHeaderCases)
|
|
|
|
}
|