1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/pkg/http/theme_test.go

122 lines
2.6 KiB
Go
Raw Normal View History

2021-10-22 22:40:03 +02:00
package http
2018-06-20 19:46:52 +02:00
import (
"io/ioutil"
"path/filepath"
2018-06-26 00:08:51 +02:00
"os"
"strings"
2018-06-20 19:46:52 +02:00
"testing"
2021-10-27 17:54:51 +00:00
"github.com/alice-lg/alice-lg/pkg/config"
2018-06-20 19:46:52 +02:00
)
func touchFile(path, filename string) error {
target := filepath.Join(path, filename)
return ioutil.WriteFile(target, []byte{}, 0644)
}
func TestThemeFiles(t *testing.T) {
themePath, err := ioutil.TempDir("", "alice-lg-tmp-theme")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(themePath)
// Create some "stylesheets" and a "script"
touchFile(themePath, "style.css")
touchFile(themePath, "extra.css")
touchFile(themePath, "script.js")
// Load theme
2021-10-27 17:54:51 +00:00
theme := NewTheme(config.ThemeConfig{
2018-06-20 19:46:52 +02:00
BasePath: "/theme",
Path: themePath,
})
if err != nil {
t.Error(err)
}
// Check file presence
scripts := theme.Scripts()
if len(scripts) != 1 {
t.Error("Expected one script file: script.js")
}
stylesheets := theme.Stylesheets()
if len(stylesheets) != 2 {
t.Error("Expected two stylesheets: {style, extra}.css")
}
// Check uri / path mapping
script := scripts[0]
2018-06-25 23:46:33 +02:00
if script != "script.js" {
t.Error("Expected script.js to be included in scripts")
2018-06-20 19:46:52 +02:00
}
}
2018-06-25 23:46:33 +02:00
func TestThemeIncludeHash(t *testing.T) {
themePath, err := ioutil.TempDir("", "alice-lg-tmp-theme")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(themePath)
// Create some "stylesheets" and a "script"
touchFile(themePath, "style.css")
2021-10-27 17:54:51 +00:00
theme := NewTheme(config.ThemeConfig{
2018-06-25 23:46:33 +02:00
BasePath: "/theme",
Path: themePath,
})
hash := theme.HashInclude("style.css")
if hash == "" {
t.Error("Something went wrong with hashing")
}
t.Log("Filehash:", hash)
}
func TestThemeIncludes(t *testing.T) {
themePath, err := ioutil.TempDir("", "alice-lg-tmp-theme")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(themePath)
// Create some "stylesheets" and a "script"
touchFile(themePath, "style.css")
touchFile(themePath, "extra.css")
touchFile(themePath, "script.js")
// Load theme
2021-10-27 17:54:51 +00:00
theme := NewTheme(config.ThemeConfig{
2018-06-25 23:46:33 +02:00
BasePath: "/theme",
Path: themePath,
})
2021-10-27 17:54:51 +00:00
stylesHTML := theme.StylesheetIncludes()
scriptsHTML := theme.ScriptIncludes()
2018-06-25 23:46:33 +02:00
2021-10-27 17:54:51 +00:00
if !strings.HasPrefix(scriptsHTML, "<script") {
2018-06-26 00:08:51 +02:00
t.Error("Script include should start with <script")
}
2021-11-15 21:43:22 +01:00
if !strings.Contains(scriptsHTML, "script.js") {
2018-06-26 00:08:51 +02:00
t.Error("Scripts include should contain script.js")
}
2021-10-27 17:54:51 +00:00
if !strings.HasPrefix(stylesHTML, "<link") {
2018-06-26 00:08:51 +02:00
t.Error("Stylesheet include should start with <link")
}
2021-11-15 21:43:22 +01:00
if !strings.Contains(stylesHTML, "extra.css") {
2018-06-26 00:08:51 +02:00
t.Error("Stylesheet include should contain extra.css")
}
2021-11-16 10:21:36 +01:00
if strings.Contains(stylesHTML, "script.js") {
2018-06-26 00:08:51 +02:00
t.Error("Stylesheet include should not contain script.js")
}
2018-06-25 23:46:33 +02:00
}