Skip to content

Commit

Permalink
templates: add InputTest
Browse files Browse the repository at this point in the history
This test goes through all TemplateSelectables listed in testInputs
and runs them through all theme templates to see if they break.
  • Loading branch information
Wessie committed Feb 21, 2024
1 parent 62299cf commit 2462b19
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
6 changes: 5 additions & 1 deletion radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (u User) ComparePassword(passwd string) error {
type DJID uint64

func (id DJID) String() string {
return strconv.Itoa(int(id))
return strconv.FormatUint(uint64(id), 10)
}

// DJ is someone that has access to streaming
Expand Down Expand Up @@ -963,6 +963,10 @@ type PendingSong struct {
AcceptedSong *Song
}

func (p PendingSong) Metadata() string {
return Metadata(p.Artist, p.Title)
}

// RelayStorage deals with the relays table.
type RelayStorage interface {
Update(r Relay) error
Expand Down
2 changes: 1 addition & 1 deletion templates/default/submit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<p>{{$song.Metadata}}</p>
</div>
<div class="column is-4">
<p class="has-text-danger">{{$song.DeclineReason}}</p>
<p class="has-text-danger">{{$song.Reason}}</p>
</div>
</div>
<hr>
Expand Down
78 changes: 78 additions & 0 deletions templates/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package templates_test

import (
"io"
"net/http/httptest"
"reflect"
"testing"

"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/website/admin"
v1 "github.com/R-a-dio/valkyrie/website/api/v1"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/R-a-dio/valkyrie/website/public"
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/arbitrary"
"github.com/leanovate/gopter/prop"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var templateInputs = []templates.TemplateSelectable{
public.HomeInput{},
public.ChatInput{},
public.LastPlayedInput{},
public.QueueInput{},
public.NewsInput{},
public.SubmitInput{},
public.SubmissionForm{},
public.FavesInput{},
public.ScheduleInput{},
public.StaffInput{},
public.SearchInput{},
middleware.LoginInput{},
v1.NowPlaying{},
v1.LastPlayed{},
v1.Queue{},
v1.SearchInput{},
admin.HomeInput{},
admin.PendingInput{},
admin.PendingForm{},
}

func TestTemplateInputs(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}

tmpl, err := templates.FromDirectory(".")
require.NoError(t, err)
tmpl.Production = true

exec := tmpl.Executor()
req := httptest.NewRequest("GET", "/", nil)

a := arbitrary.DefaultArbitraries()
param := gopter.DefaultTestParameters()
param.MinSuccessfulTests = 100
p := gopter.NewProperties(param)

for _, theme := range tmpl.ThemeNames() {
req = req.WithContext(templates.SetTheme(req.Context(), theme))
for _, in := range templateInputs {
rtyp := reflect.TypeOf(in)
p.Property(theme+"-"+rtyp.Name(), prop.ForAll(
func(a any) bool {
// some types used for TemplateSelectable are just aliases/simple renames
// and those loose their "proper" type when gopter generates them so we
// need to convert it back into the type we expect
v := reflect.ValueOf(a).Convert(rtyp)
input := v.Interface().(templates.TemplateSelectable)
return assert.NoError(t, exec.Execute(io.Discard, req, input))
},
a.GenForType(rtyp),
))
}
}
p.TestingRun(t)
}
5 changes: 5 additions & 0 deletions templates/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (

// Site is an overarching struct containing all the themes of the website.
type Site struct {
Production bool

fs fs.FS

mu sync.RWMutex
Expand Down Expand Up @@ -72,6 +74,9 @@ func (s *Site) ThemeNames() []string {
//
// If theme does not exist it uses the default-theme
func (s *Site) Template(theme, page string) (*template.Template, error) {
if s.Production {
return s.prodTemplate(theme, page)
}
return s.devTemplate(theme, page)
}

Expand Down
7 changes: 5 additions & 2 deletions templates/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func ThemeCtx(storage radio.StorageService) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

ctx = context.WithValue(ctx, themeKey{}, "default")
ctx = SetTheme(ctx, "default")
next.ServeHTTP(w, r.WithContext(ctx))
})
}
Expand All @@ -35,3 +34,7 @@ func GetTheme(ctx context.Context) string {

return theme
}

func SetTheme(ctx context.Context, theme string) context.Context {
return context.WithValue(ctx, themeKey{}, theme)
}
7 changes: 7 additions & 0 deletions website/shared/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type Pagination struct {
}

func (p *Pagination) URL() template.URL {
if p == nil || p.uri == nil {
return template.URL("")
}

u := *p.uri
v := u.Query()
v.Set("page", strconv.FormatInt(p.Nr, 10))
Expand All @@ -37,6 +41,9 @@ func (p *Pagination) URL() template.URL {
}

func (p *Pagination) BaseURL() template.URL {
if p == nil || p.uri == nil {
return template.URL("")
}
return template.URL(p.uri.Path)
}

Expand Down

0 comments on commit 2462b19

Please sign in to comment.