2017-06-28 13:42:38 +02:00
package controllers
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"path"
"testing"
"strings"
2017-07-13 19:45:09 +02:00
"time"
2017-06-28 13:42:38 +02:00
"github.com/CloudyKit/jet"
"github.com/NyaaPantsu/nyaa/config"
2017-07-03 00:34:18 +02:00
"github.com/NyaaPantsu/nyaa/models"
2017-07-13 19:45:09 +02:00
"github.com/NyaaPantsu/nyaa/utils/filelist"
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 ) {
2017-07-13 19:45:09 +02:00
fakeUser := & models . User { 1 , "test" , "test" , "test" , 1 , time . Now ( ) , time . Now ( ) , "test" , time . Now ( ) , "en" , "test" , "test" , "test" , "test" , [ ] models . User { } , [ ] models . User { } , "test" , [ ] models . Torrent { } , [ ] models . Notification { } , 1 , models . UserSettings { } }
fakeComment := & models . Comment { 1 , 1 , 1 , "test" , time . Now ( ) , time . Now ( ) , nil , & models . Torrent { } , fakeUser }
fakeScrapeData := & models . Scrape { 1 , 0 , 0 , 10 , time . Now ( ) }
fakeFile := & models . File { 1 , 1 , "l12:somefile.mp4e" , 3 }
fakeLanguages := [ ] string { "fr" , "en" }
fakeTorrent := & models . Torrent { 1 , "test" , "test" , 3 , 12 , 1 , false , time . Now ( ) , 1 , 0 , 3 , "test" , "test" , "test" , "test" , "test" , nil , fakeUser , "test" , [ ] models . OldComment { } , [ ] models . Comment { * fakeComment , * fakeComment } , fakeScrapeData , [ ] models . File { * fakeFile } , fakeLanguages }
fakeActivity := & models . Activity { 1 , "t" , "e" , "s" , 1 , fakeUser }
fakeDB := & models . DatabaseDump { time . Now ( ) , 3 , "test" , "test" }
fakeLanguage := & publicSettings . Language { "English" , "en" , "en-us" }
fakeTorrentRequest := & torrentValidator . TorrentRequest { Name : "test" , Magnet : "" , Category : "" , Remake : false , Description : "" , Status : 1 , Hidden : false , CaptchaID : "" , WebsiteLink : "" , SubCategory : 0 , Languages : nil , Infohash : "" , SubCategoryID : 0 , CategoryID : 0 , Filesize : 0 , Filepath : "" , FileList : nil , Trackers : nil }
fakeLogin := & userValidator . LoginForm { "test" , "test" , "/" }
fakeRegistration := & userValidator . RegistrationForm { "test" , "" , "test" , "test" , "xxxx" , "1" }
fakeReport := & models . TorrentReport { 1 , "test" , 1 , 1 , time . Now ( ) , fakeTorrent , fakeUser }
2017-06-28 13:42:38 +02:00
contextVars := ContextTest {
"dumps.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "GPGLink" , "test" )
vars . Set ( "ListDumps" , [ ] models . DatabaseDumpJSON { fakeDB . ToJSON ( ) , fakeDB . ToJSON ( ) } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"activities.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . Activity { * fakeActivity } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"listing.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . TorrentJSON { fakeTorrent . ToJSON ( ) , fakeTorrent . ToJSON ( ) } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"edit.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , fakeTorrentRequest )
vars . Set ( "Languages" , publicSettings . Languages { * fakeLanguage , * fakeLanguage } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"upload.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , fakeTorrentRequest )
2017-06-28 13:42:38 +02:00
return vars
} ,
"view.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Torrent" , fakeTorrent . ToJSON ( ) )
2017-06-28 13:42:38 +02:00
vars . Set ( "CaptchaID" , "xxxxxx" )
2017-07-13 19:45:09 +02:00
vars . Set ( "RootFolder" , filelist . FileListToFolder ( fakeTorrent . FileList , "root" ) )
2017-06-28 13:42:38 +02:00
return vars
} ,
"settings.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , & LanguagesJSONResponse { "test" , publicSettings . Languages { * fakeLanguage , * fakeLanguage } } )
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 {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , fakeLogin )
2017-07-04 02:54:43 +02:00
return vars
} ,
2017-06-28 13:42:38 +02:00
"register.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , fakeRegistration )
2017-06-28 13:42:38 +02:00
return vars
} ,
"index.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Torrents" , [ ] models . Torrent { * fakeTorrent , * fakeTorrent } )
vars . Set ( "Users" , [ ] models . User { * fakeUser , * fakeUser } )
vars . Set ( "Comments" , [ ] models . Comment { * fakeComment , * fakeComment } )
vars . Set ( "TorrentReports" , [ ] models . TorrentReportJSON { fakeReport . ToJSON ( ) , fakeReport . ToJSON ( ) } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"paneltorrentedit.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , * fakeTorrent )
2017-06-28 13:42:38 +02:00
return vars
} ,
"reassign.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , ReassignForm { 1 , "" , "" , [ ] uint { 1 , 1 } } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"torrentlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . Torrent { * fakeTorrent , * fakeTorrent } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"userlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . User { * fakeUser , * fakeUser } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"commentlist.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . Comment { * fakeComment , * fakeComment } )
2017-06-28 13:42:38 +02:00
return vars
} ,
"torrent_report.jet.html" : func ( vars jet . VarMap ) jet . VarMap {
2017-07-13 19:45:09 +02:00
vars . Set ( "Models" , [ ] models . TorrentReportJSON { fakeReport . ToJSON ( ) , fakeReport . ToJSON ( ) } )
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
}
2017-07-13 19:45:09 +02:00
vars . Set ( "Form" , form { 1 , "test" } )
2017-07-04 01:22:12 +02:00
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" ,
2017-07-13 19:45:09 +02:00
DateType : "test" ,
MinSize : "test" ,
MaxSize : "test" ,
FromDate : "test" ,
ToDate : "test" ,
2017-06-28 13:42:38 +02:00
} )
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 )
}
2017-07-13 19:45:09 +02:00
Ts , _ , err := publicSettings . TfuncAndLanguageWithFallback ( "en-us" , "test" , "test" )
2017-06-28 13:42:38 +02:00
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 )
2017-07-13 19:45:09 +02:00
vars . Set ( "Theme" , "test" )
vars . Set ( "Mascot" , "test" )
vars . Set ( "MascotURL" , "test" )
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
}