1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 10:06:24 -07:00

Add tests for banpage.html template

This commit is contained in:
Eggbertx 2024-04-02 11:33:13 -07:00
parent e44dc78ae5
commit e446f2a5d7
4 changed files with 226 additions and 50 deletions

View file

@ -133,8 +133,8 @@ type Upload struct {
Height int // sql: `height`
}
// used to composition IPBan and IPBanAudit
type ipBanBase struct {
// IPBanBase used to composition IPBan and IPBanAudit. It does not represent a SQL table by itself
type IPBanBase struct {
IsActive bool
IsThreadBan bool
ExpiresAt time.Time
@ -156,7 +156,7 @@ type IPBan struct {
RangeStart string
RangeEnd string
IssuedAt time.Time
ipBanBase
IPBanBase
}
// IP was previously a field in the IPBan struct before range bans were
@ -190,7 +190,7 @@ func (ipb *IPBan) IsBanned(ipStr string) (bool, error) {
type IPBanAudit struct {
IPBanID int // sql: `ip_ban_id`
Timestamp time.Time // sql: `timestamp`
ipBanBase
IPBanBase
}
// used to composition IPBanAppeal and IPBanAppealAudit

View file

@ -1,8 +1,20 @@
package gctemplates_test
import "github.com/gochan-org/gochan/pkg/config"
import (
"bytes"
"testing"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/server/serverutil"
"github.com/stretchr/testify/assert"
)
var (
testingSiteConfig = config.SiteConfig{
SiteName: "Gochan",
SiteSlogan: "Gochan test",
}
jsConstsCases = []templateTestCase{
{
desc: "base test",
@ -27,4 +39,182 @@ var (
expectedOutput: `var styles=[];var defaultStyle="";var webroot="";var serverTZ=0;`,
},
}
banPageCases = []templateTestCase{
{
desc: "appealable permaban",
data: map[string]any{
"ban": &gcsql.IPBan{
RangeStart: "192.168.56.0",
RangeEnd: "192.168.56.255",
IPBanBase: gcsql.IPBanBase{
Permanent: true,
CanAppeal: true,
StaffID: 1,
Message: "ban message goes here",
},
},
"ip": "192.168.56.1",
"siteConfig": testingSiteConfig,
"systemCritical": config.SystemCriticalConfig{
WebRoot: "/",
},
"boardConfig": config.BoardConfig{
DefaultStyle: "pipes.css",
},
},
expectedOutput: `<!DOCTYPE html><html><head><title>Banned</title>` +
`<link rel="shortcut icon"href="/favicon.png">` +
`<link rel="stylesheet"href="/css/global.css"/>` +
`<link id="theme"rel="stylesheet"href="/css/pipes.css"/>` +
`<script type="text/javascript"src="/js/consts.js"></script>` +
`<script type="text/javascript"src="/js/gochan.js"></script></head>` +
`<body><div id="top-pane"><span id="site-title">Gochan</span><br /><span id="site-slogan">Gochan test</span></div><br />` +
`<div class="section-block"style="margin: 0px 26px 0px 24px">` +
`<div class="section-title-block"><span class="section-title"><b>YOU ARE BANNED&nbsp;:(</b></span></div>` +
`<div class="section-body"style="padding-top:8px"><div id="ban-info"style="float:left">You are banned from posting on<b>all boards</b>for the following reason:<br/><br/>` +
`<b>ban message goes here</b><br/><br/>` +
`Your ban was placed on Mon,January 01,0001 12:00:00 AM and will<b>not expire</b>.<br />` +
`Your IP address is<b>192.168.56.1</b>.<br /><br/>You may appeal this ban:<br/>` +
`<form id="appeal-form"action="/post"method="POST">` +
`<input type="hidden"name="board"value=""><input type="hidden"name="banid"value="0">` +
`<textarea rows="4"cols="48"name="appealmsg"id="postmsg"placeholder="Appeal message"></textarea><br />` +
`<input type="submit"name="doappeal"value="Submit"/><br/></form></div></div></div>` +
`<div id="footer">Powered by<a href="http://github.com/gochan-org/gochan/">Gochan 3.10.1</a><br /></div></div></body></html>`,
},
{
desc: "unappealable permaban (banned forever)",
data: map[string]any{
"ban": &gcsql.IPBan{
RangeStart: "192.168.56.0",
RangeEnd: "192.168.56.255",
IPBanBase: gcsql.IPBanBase{
IsActive: true,
Permanent: true,
StaffID: 1,
Message: "ban message goes here",
},
},
"ip": "192.168.56.1",
"siteConfig": testingSiteConfig,
"systemCritical": config.SystemCriticalConfig{
WebRoot: "/",
},
"boardConfig": config.BoardConfig{
DefaultStyle: "pipes.css",
},
},
expectedOutput: `<!DOCTYPE html><html><head><title>Banned</title>` +
`<link rel="shortcut icon"href="/favicon.png">` +
`<link rel="stylesheet"href="/css/global.css"/>` +
`<link id="theme"rel="stylesheet"href="/css/pipes.css"/>` +
`<script type="text/javascript"src="/js/consts.js"></script>` +
`<script type="text/javascript"src="/js/gochan.js"></script></head>` +
`<body><div id="top-pane"><span id="site-title">Gochan</span><br /><span id="site-slogan">Gochan test</span></div><br />` +
`<div class="section-block"style="margin: 0px 26px 0px 24px"><div class="section-title-block">` +
`<span class="section-title"><b>YOUR'E PERMABANNED,IDIOT!</b></span></div>` +
`<div class="section-body"style="padding-top:8px"><div id="ban-info"style="float:left">You are banned from posting on<b>all boards</b>for the following reason:<br/><br/>` +
`<b>ban message goes here</b><br/><br/>Your ban was placed on Mon,January 01,0001 12:00:00 AM and will<b>not expire</b>.<br />` +
`Your IP address is<b>192.168.56.1</b>.<br /><br/>You may&nbsp;<b>not</b> appeal this ban.<br /></div>` +
`<img id="banpage-image"src="/permabanned.jpg"style="float:right; margin: 4px 8px 8px 4px"/><br/>` +
`<audio id="jack"preload="auto"autobuffer loop><source src="/static/hittheroad.ogg"/><source src="/static/hittheroad.wav"/><source src="/static/hittheroad.mp3"/></audio>` +
`<script type="text/javascript">document.getElementById("jack").play();</script></div></div>` +
`<div id="footer">Powered by<a href="http://github.com/gochan-org/gochan/">Gochan 3.10.1</a><br /></div></div></body></html>`,
},
{
desc: "appealable temporary ban",
data: map[string]any{
"ban": &gcsql.IPBan{
RangeStart: "192.168.56.0",
RangeEnd: "192.168.56.255",
IPBanBase: gcsql.IPBanBase{
CanAppeal: true,
StaffID: 1,
Message: "ban message goes here",
},
},
"ip": "192.168.56.1",
"siteConfig": testingSiteConfig,
"systemCritical": config.SystemCriticalConfig{
WebRoot: "/",
},
"boardConfig": config.BoardConfig{
DefaultStyle: "pipes.css",
},
},
expectedOutput: `<!DOCTYPE html><html><head><title>Banned</title>` +
`<link rel="shortcut icon"href="/favicon.png">` +
`<link rel="stylesheet"href="/css/global.css"/>` +
`<link id="theme"rel="stylesheet"href="/css/pipes.css"/>` +
`<script type="text/javascript"src="/js/consts.js"></script>` +
`<script type="text/javascript"src="/js/gochan.js"></script></head>` +
`<body><div id="top-pane"><span id="site-title">Gochan</span><br /><span id="site-slogan">Gochan test</span></div><br />` +
`<div class="section-block"style="margin: 0px 26px 0px 24px">` +
`<div class="section-title-block"><span class="section-title"><b>YOU ARE BANNED&nbsp;:(</b></span></div>` +
`<div class="section-body"style="padding-top:8px"><div id="ban-info"style="float:left">You are banned from posting on<b>all boards</b>for the following reason:<br/><br/>` +
`<b>ban message goes here</b><br/><br/>` +
`Your ban was placed on Mon,January 01,0001 12:00:00 AM and will expire on&nbsp;<b>Mon,January 01,0001 12:00:00 AM</b>.<br />` +
`Your IP address is<b>192.168.56.1</b>.<br /><br/>You may appeal this ban:<br/>` +
`<form id="appeal-form"action="/post"method="POST">` +
`<input type="hidden"name="board"value=""><input type="hidden"name="banid"value="0">` +
`<textarea rows="4"cols="48"name="appealmsg"id="postmsg"placeholder="Appeal message"></textarea><br />` +
`<input type="submit"name="doappeal"value="Submit"/><br/></form></div></div></div>` +
`<div id="footer">Powered by<a href="http://github.com/gochan-org/gochan/">Gochan 3.10.1</a><br /></div></div></body></html>`,
},
{
desc: "unappealable temporary ban",
data: map[string]any{
"ban": &gcsql.IPBan{
RangeStart: "192.168.56.0",
RangeEnd: "192.168.56.255",
IPBanBase: gcsql.IPBanBase{
StaffID: 1,
Message: "ban message goes here",
},
},
"ip": "192.168.56.1",
"siteConfig": testingSiteConfig,
"systemCritical": config.SystemCriticalConfig{
WebRoot: "/",
},
"boardConfig": config.BoardConfig{
DefaultStyle: "pipes.css",
},
},
expectedOutput: `<!DOCTYPE html><html><head><title>Banned</title>` +
`<link rel="shortcut icon"href="/favicon.png">` +
`<link rel="stylesheet"href="/css/global.css"/>` +
`<link id="theme"rel="stylesheet"href="/css/pipes.css"/>` +
`<script type="text/javascript"src="/js/consts.js"></script>` +
`<script type="text/javascript"src="/js/gochan.js"></script></head>` +
`<body><div id="top-pane"><span id="site-title">Gochan</span><br /><span id="site-slogan">Gochan test</span></div><br />` +
`<div class="section-block"style="margin: 0px 26px 0px 24px">` +
`<div class="section-title-block"><span class="section-title"><b>YOU ARE BANNED&nbsp;:(</b></span></div>` +
`<div class="section-body"style="padding-top:8px"><div id="ban-info"style="float:left">You are banned from posting on<b>all boards</b>for the following reason:<br/><br/>` +
`<b>ban message goes here</b><br/><br/>` +
`Your ban was placed on Mon,January 01,0001 12:00:00 AM and will expire on&nbsp;<b>Mon,January 01,0001 12:00:00 AM</b>.<br />` +
`Your IP address is<b>192.168.56.1</b>.<br /><br/>You may&nbsp;<b>not</b> appeal this ban.<br />` +
`</div></div></div>` +
`<div id="footer">Powered by<a href="http://github.com/gochan-org/gochan/">Gochan 3.10.1</a><br /></div></div></body></html>`,
},
}
)
type templateTestCase struct {
desc string
data any
expectsError bool
expectedOutput string
}
func (tC *templateTestCase) Run(t *testing.T, templateName string) {
buf := new(bytes.Buffer)
err := serverutil.MinifyTemplate(templateName, tC.data, buf, "text/javascript")
if tC.expectsError {
assert.Error(t, err)
} else {
if !assert.NoError(t, err) {
return
}
assert.Equal(t, tC.expectedOutput, buf.String())
}
}

View file

@ -1,7 +1,6 @@
package gctemplates_test
import (
"bytes"
"testing"
"time"
@ -28,15 +27,13 @@ const (
`ORDER BY\s+position ASC,\s*name ASC`
)
type templateTestCase struct {
desc string
data any
expectsError bool
expectedOutput string
}
func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock, which ...string) bool {
t.Helper()
_, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
return false
}
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",
@ -55,22 +52,19 @@ func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock, which ...string) bool
config.SetTestTemplateDir("templates")
err := gctemplates.InitTemplates(which...)
if !assert.NoError(t, err) {
if !assert.NoError(t, gctemplates.InitTemplates(which...)) {
return false
}
return assert.NoError(t, mock.ExpectationsWereMet())
}
func TestJsConstsTemplate(t *testing.T) {
_, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
return
}
func runTemplateTestCases(t *testing.T, templateName string, testCases []templateTestCase) {
t.Helper()
db, mock, err := sqlmock.New()
if !assert.NoError(t, err) {
return
}
config.SetVersion("3.10.1")
config.SetTestDBConfig("mysql", "localhost", "gochan", "gochan", "gochan", "")
if !assert.NoError(t, gcsql.SetTestingDB("mysql", "gochan", "", db)) {
return
@ -81,29 +75,22 @@ func TestJsConstsTemplate(t *testing.T) {
}
serverutil.InitMinifier()
buf := new(bytes.Buffer)
for _, tC := range jsConstsCases {
buf.Reset()
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
err := serverutil.MinifyTemplate(gctemplates.JsConsts, tC.data, buf, "text/javascript")
if tC.expectsError {
assert.Error(t, err)
} else {
if !assert.NoError(t, err) {
return
}
}
assert.Equal(t, tC.expectedOutput, buf.String())
tC.Run(t, templateName)
})
}
}
func TestBanPageTemplate(t *testing.T) {
runTemplateTestCases(t, gctemplates.BanPage, banPageCases)
}
func TestJsConstsTemplate(t *testing.T) {
runTemplateTestCases(t, gctemplates.JsConsts, jsConstsCases)
}
func TestTemplateBase(t *testing.T) {
_, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
return
}
db, mock, err := sqlmock.New()
if !assert.NoError(t, err) {
return

View file

@ -2,11 +2,11 @@
<html>
<head>
<title>Banned</title>
<link rel="shortcut icon" href="{{.systemCritical.WebRoot}}favicon.png">
<link rel="stylesheet" href="{{.systemCritical.WebRoot}}css/global.css" />
<link id="theme" rel="stylesheet" href="{{.systemCritical.WebRoot}}css/{{.boardConfig.DefaultStyle}}" />
<script type="text/javascript" src="{{.systemCritical.WebRoot}}js/consts.js"></script>
<script type="text/javascript" src="{{.systemCritical.WebRoot}}js/gochan.js"></script>
<link rel="shortcut icon" href="{{webPath `favicon.png`}}">
<link rel="stylesheet" href="{{webPath `css/global.css`}}" />
<link id="theme" rel="stylesheet" href="{{webPath `css` .boardConfig.DefaultStyle}}" />
<script type="text/javascript" src="{{webPath `js/consts.js`}}"></script>
<script type="text/javascript" src="{{webPath `js/gochan.js`}}"></script>
</head>
<body>
<div id="top-pane">
@ -15,7 +15,7 @@
</div><br />
<div class="section-block" style="margin: 0px 26px 0px 24px">
<div class="section-title-block">
<span class="section-title"><b>{{if .ban.BannedForever}}YOUR'E PERMABANNED, IDIOT!{{else}}YOU ARE BANNED :({{end}}</b></span>
<span class="section-title"><b>{{if .ban.BannedForever}}YOUR'E PERMABANNED, IDIOT!{{else}}YOU ARE BANNED&nbsp;:({{end}}</b></span>
</div>
<div class="section-body" style="padding-top:8px">
<div id="ban-info" style="float:left">{{if .ban.IsGlobalBan}}
@ -24,22 +24,21 @@
<br /><br />
<b>{{.ban.Message}}</b>
<br /><br />{{$expiresTimestamp := formatTimestamp .ban.ExpiresAt}}{{$appealTimestamp := formatTimestamp .ban.AppealAt}}
Your ban was placed on {{formatTimestamp .ban.IssuedAt}} and will
{{if .ban.Permanent}}<b>not expire</b>{{else}}expire on <b>{{$expiresTimestamp}}</b>{{end}}.<br />
Your ban was placed on {{formatTimestamp .ban.IssuedAt}} and will {{if .ban.Permanent}}<b>not expire</b>{{else}}expire on&nbsp;<b>{{$expiresTimestamp}}</b>{{end}}.<br />
Your IP address is <b>{{.ip}}</b>.<br /><br />
{{if .ban.CanAppeal}}You may appeal this ban:<br />
<form id="appeal-form" action="{{webPath "/post"}}" method="POST">
<form id="appeal-form" action="{{webPath `/post`}}" method="POST">
<input type="hidden" name="board" value="{{.board.Dir}}">
<input type="hidden" name="banid" value="{{.ban.ID}}">
<textarea rows="4" cols="48" name="appealmsg" id="postmsg" placeholder="Appeal message"></textarea><br />
<input type="submit" name="doappeal" value="Submit" /><br />
</form>{{else}}You may <b>not</b> appeal this ban.<br />{{end}}
</form>{{else}}You may&nbsp;<b>not</b> appeal this ban.<br />{{end}}
</div>{{if .ban.BannedForever}}
<img id="banpage-image" src="{{webPath "permabanned.jpg"}}" style="float:right; margin: 4px 8px 8px 4px"/><br />
<audio id="jack" preload="auto" autobuffer loop>
<source src="{{webPath "static/hittheroad.ogg"}}" />
<source src="{{webPath "static/hittheroad.wav"}}" />
<source src="{{webPath "static/hittheroad.mp3"}}" />
<source src="{{webPath `static/hittheroad.ogg`}}" />
<source src="{{webPath `static/hittheroad.wav`}}" />
<source src="{{webPath `static/hittheroad.mp3`}}" />
</audio>
<script type="text/javascript">
document.getElementById("jack").play();