2017-06-28 13:42:38 +02:00
package controllers
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"path"
"testing"
"strings"
"github.com/CloudyKit/jet"
"github.com/NyaaPantsu/nyaa/config"
2017-07-03 00:34:18 +02:00
"github.com/NyaaPantsu/nyaa/models"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
2017-07-03 00:34:18 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
"github.com/NyaaPantsu/nyaa/utils/validator/user"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
)
// run before router/init.go:init()
var _ = func ( ) ( _ struct { } ) {
gin . SetMode ( gin . TestMode )
config . ConfigPath = path . Join ( ".." , config . ConfigPath )
config . DefaultConfigPath = path . Join ( ".." , config . DefaultConfigPath )
2017-07-10 14:11:05 +02:00
config . Reload ( )
2017-06-28 13:42:38 +02:00
return
} ( )
func TestTemplates ( t * testing . T ) {
//var View = jet.NewHTMLSet(TemplateDir)
fmt . Print ( "JetTest Template started\n" )
walkDirTest ( "/" , t )
}
type ContextTest map [ string ] func ( jet . VarMap ) jet . VarMap
func walkDirTest ( dir string , t * testing . T ) {
contextVars := ContextTest {
"dumps.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
vars . Set ( "GPGLink" , "" )
2017-07-01 23:09:35 +02:00
vars . Set ( "ListDumps" , [ ] models . DatabaseDumpJSON { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"activities.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . Activity { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"listing.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . TorrentJSON { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"edit.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-03 00:34:18 +02:00
vars . Set ( "Form" , & torrentValidator . TorrentRequest { } )
2017-07-11 22:48:52 +02:00
vars . Set ( "Languages" , publicSettings . Languages { { "" , "" , "" } } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"upload.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-03 00:34:18 +02:00
vars . Set ( "Form" , & torrentValidator . TorrentRequest { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"view.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Torrent" , & models . TorrentJSON { } )
2017-06-28 13:42:38 +02:00
vars . Set ( "CaptchaID" , "xxxxxx" )
return vars
} ,
"settings.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-11 22:48:52 +02:00
vars . Set ( "Form" , & LanguagesJSONResponse { "" , publicSettings . Languages { { "" , "" , "" } } } )
2017-06-28 13:42:38 +02:00
return vars
} ,
2017-07-04 02:54:43 +02:00
"login.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
vars . Set ( "Form" , & userValidator . LoginForm { } )
return vars
} ,
2017-06-28 13:42:38 +02:00
"register.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-03 00:34:18 +02:00
vars . Set ( "Form" , & userValidator . RegistrationForm { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"index.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Torrents" , [ ] models . Torrent { } )
vars . Set ( "Users" , [ ] models . User { } )
vars . Set ( "Comments" , [ ] models . Comment { } )
vars . Set ( "TorrentReports" , [ ] models . TorrentReportJSON { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"paneltorrentedit.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Form" , models . Torrent { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"reassign.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
vars . Set ( "Form" , ReassignForm { } )
return vars
} ,
"torrentlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . Torrent { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"userlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . User { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"commentlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . Comment { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"torrent_report.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-01 23:09:35 +02:00
vars . Set ( "Models" , [ ] models . TorrentReportJSON { } )
2017-06-28 13:42:38 +02:00
return vars
} ,
2017-07-04 01:22:12 +02:00
"report.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
type form struct {
2017-07-04 01:23:17 +02:00
ID int
CaptchaID string
2017-07-04 01:22:12 +02:00
}
vars . Set ( "Form" , form { 1 , "" } )
return vars
} ,
2017-06-28 13:42:38 +02:00
}
fmt . Printf ( "\nTesting Folder: %s\n" , dir )
view := jet . NewHTMLSet ( path . Join ( ".." , TemplateDir ) )
vars := mockupCommonVars ( t )
files , err := ioutil . ReadDir ( path . Join ( ".." , TemplateDir ) + dir )
if err != nil {
t . Errorf ( "Couldn't find the folder %s" , path . Join ( ".." , TemplateDir ) + dir )
}
if len ( files ) == 0 {
t . Errorf ( "Couldn't find any files in folder %s" , path . Join ( ".." , TemplateDir ) + dir )
}
for _ , f := range files {
if f . IsDir ( ) {
walkDirTest ( dir + f . Name ( ) + "/" , t )
continue
}
if strings . Contains ( f . Name ( ) , ".jet.html" ) {
template , err := view . GetTemplate ( dir + f . Name ( ) )
fmt . Printf ( "\tJetTest Template of: %s" , dir + f . Name ( ) )
if err != nil {
t . Errorf ( "\nParsing error: %s %s" , err . Error ( ) , dir + f . Name ( ) )
}
buff := bytes . NewBuffer ( nil )
if contextVars [ f . Name ( ) ] != nil {
vars = contextVars [ f . Name ( ) ] ( vars )
}
if err = template . Execute ( buff , vars , nil ) ; err != nil {
t . Errorf ( "\nEval error: %q executing %s" , err . Error ( ) , template . Name )
}
fmt . Print ( "\tOK\n" )
}
}
}
func mockupCommonVars ( t * testing . T ) jet . VarMap {
vars . Set ( "Navigation" , newNavigation ( ) )
vars . Set ( "Search" , searchForm {
Category : "_" ,
ShowItemsPerPage : true ,
SizeType : "b" ,
DateType : "" ,
MinSize : "" ,
MaxSize : "" ,
FromDate : "" ,
ToDate : "" ,
} )
2017-07-10 14:11:05 +02:00
conf := config . Get ( ) . I18n
2017-06-28 13:42:38 +02:00
conf . Directory = path . Join ( ".." , conf . Directory )
var retriever publicSettings . UserRetriever // not required during initialization
err := publicSettings . InitI18n ( conf , retriever )
if err != nil {
t . Errorf ( "failed to initialize language translations: %v" , err )
}
Ts , _ , err := publicSettings . TfuncAndLanguageWithFallback ( "en-us" , "" , "" )
if err != nil {
t . Error ( "Couldn't load language files!" )
}
var T publicSettings . TemplateTfunc
T = func ( id string , args ... interface { } ) template . HTML {
return template . HTML ( fmt . Sprintf ( Ts ( id ) , args ... ) )
}
vars . Set ( "T" , T )
vars . Set ( "Theme" , "" )
vars . Set ( "Mascot" , "" )
vars . Set ( "MascotURL" , "" )
2017-07-01 23:09:35 +02:00
vars . Set ( "User" , & models . User { } )
vars . Set ( "UserProfile" , & models . User { } )
2017-06-28 13:42:38 +02:00
vars . Set ( "URL" , & url . URL { } )
vars . Set ( "CsrfToken" , "xxxxxx" )
2017-07-10 14:11:05 +02:00
vars . Set ( "Config" , config . Get ( ) )
2017-06-28 13:42:38 +02:00
vars . Set ( "Infos" , make ( map [ string ] [ ] string ) )
vars . Set ( "Errors" , make ( map [ string ] [ ] string ) )
2017-07-01 23:09:35 +02:00
vars . Set ( "UserProfile" , & models . User { } )
2017-06-28 13:42:38 +02:00
return vars
}