aboutsummaryrefslogtreecommitdiff
path: root/quartzgun_test.go
blob: b555d90cd1b952e59b478aaefd68d1b6c11c4482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	"context"
	"fmt"
	"html/template"
	"net/http"
	"nilfm.cc/git/quartzgun/indentalUserDB"
	"nilfm.cc/git/quartzgun/middleware"
	"nilfm.cc/git/quartzgun/renderer"
	"nilfm.cc/git/quartzgun/router"
	"testing"
)

func AddContent(next http.Handler) http.Handler {
	handlerFunc := func(w http.ResponseWriter, req *http.Request) {
		if !req.Form.Has("Content") {
			req.Form.Add("Content", "Yesssssss")
		}
		next.ServeHTTP(w, req)
	}
	return http.HandlerFunc(handlerFunc)
}

func ApiSomething(next http.Handler) http.Handler {
	handlerFunc := func(w http.ResponseWriter, req *http.Request) {
		*req = *req.WithContext(context.WithValue(req.Context(), "apiData", "something"))
		next.ServeHTTP(w, req)
	}

	return http.HandlerFunc(handlerFunc)
}

func TestMain(m *testing.M) {
	udb := indentalUserDB.CreateIndentalUserDB("testData/userDB.ndtl")
	udb.AddUser("nilix", "questing")
	sesh, _ := udb.InitiateSession("nilix", "questing", 60)

	fmt.Printf("%s // %s\n", sesh, sesh)
	rtr := &router.Router{
		StaticPaths: map[string]string{
			"/static": "testData/static",
		},
		Fallback: *template.Must(template.ParseFiles("testData/templates/error.html", "testData/templates/footer.html")),
	}

	rtr.Get("/login", renderer.Template(
		"testData/templates/login.html"))

	rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1", 120))

  rtr.Post("/provision", middleware.Provision(udb, 60))
  rtr.Get("/protected", middleware.Validate(renderer.Template("testData/templates/test.html"), udb, map[string]string{}))

	rtr.Get("/", middleware.Protected(
		renderer.Template(
			"testData/templates/test.html"), http.MethodGet, udb, "/login"))

	rtr.Get("/json", ApiSomething(renderer.JSON("apiData")))

	rtr.Get(`/thing/(?P<Thing>\w+)`, renderer.Template("testData/templates/paramTest.html"))

	http.ListenAndServe(":8080", rtr)
}