| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package frontend | |
| 6 | |
| 7 import ( | |
| 8 "flag" | |
| 9 "fmt" | |
| 10 "io/ioutil" | |
| 11 "net/http" | |
| 12 "net/url" | |
| 13 "path/filepath" | |
| 14 "reflect" | |
| 15 "regexp" | |
| 16 "strings" | |
| 17 "testing" | |
| 18 "time" | |
| 19 | |
| 20 "github.com/luci/gae/impl/memory" | |
| 21 "github.com/luci/luci-go/common/clock/testclock" | |
| 22 "github.com/luci/luci-go/milo/appengine/buildbot" | |
| 23 "github.com/luci/luci-go/milo/appengine/settings" | |
| 24 "github.com/luci/luci-go/milo/appengine/swarming" | |
| 25 "github.com/luci/luci-go/server/auth" | |
| 26 "github.com/luci/luci-go/server/auth/identity" | |
| 27 luciSettings "github.com/luci/luci-go/server/settings" | |
| 28 . "github.com/smartystreets/goconvey/convey" | |
| 29 "golang.org/x/net/context" | |
| 30 ) | |
| 31 | |
| 32 var ( | |
| 33 allHandlers = []settings.TestableHandler{ | |
| 34 settings.TestableSettings{}, | |
| 35 buildbot.TestableBuild{}, | |
| 36 buildbot.TestableBuilder{}, | |
| 37 swarming.TestableBuild{}, | |
| 38 swarming.TestableLog{}, | |
| 39 testableFrontpage{}, | |
| 40 } | |
| 41 ) | |
| 42 | |
| 43 var generate = flag.Bool( | |
| 44 "test.generate", false, "Generate expectations instead of running tests.
") | |
| 45 | |
| 46 func expectFileName(name string) string { | |
| 47 name = strings.Replace(name, " ", "_", -1) | |
| 48 name = strings.Replace(name, "/", "_", -1) | |
| 49 name = strings.Replace(name, ":", "-", -1) | |
| 50 return filepath.Join("expectations", name) | |
| 51 } | |
| 52 | |
| 53 func load(name string) ([]byte, error) { | |
| 54 filename := expectFileName(name) | |
| 55 return ioutil.ReadFile(filename) | |
| 56 } | |
| 57 | |
| 58 // mustWrite Writes a buffer into an expectation file. Should always work or | |
| 59 // panic. This is fine because this only runs when -generate is passed in, | |
| 60 // not during tests. | |
| 61 func mustWrite(name string, buf []byte) { | |
| 62 filename := expectFileName(name) | |
| 63 err := ioutil.WriteFile(filename, buf, 0644) | |
| 64 if err != nil { | |
| 65 panic(err) | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // fakeOAuthMethod implements Method. | |
| 70 type fakeOAuthMethod struct { | |
| 71 clientID string | |
| 72 } | |
| 73 | |
| 74 func (m fakeOAuthMethod) Authenticate(context.Context, *http.Request) (*auth.Use
r, error) { | |
| 75 return &auth.User{ | |
| 76 Identity: identity.Identity("user:abc@example.com"), | |
| 77 Email: "abc@example.com", | |
| 78 ClientID: m.clientID, | |
| 79 }, nil | |
| 80 } | |
| 81 | |
| 82 func (m fakeOAuthMethod) LoginURL(c context.Context, target string) (string, err
or) { | |
| 83 return "https://login.url/?target=" + target, nil | |
| 84 } | |
| 85 | |
| 86 func (m fakeOAuthMethod) LogoutURL(c context.Context, target string) (string, er
ror) { | |
| 87 return "https://logout.url/?target=" + target, nil | |
| 88 } | |
| 89 | |
| 90 type analyticsSettings struct { | |
| 91 AnalyticsID string `json:"analytics_id"` | |
| 92 } | |
| 93 | |
| 94 func TestPages(t *testing.T) { | |
| 95 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) | |
| 96 fixZeroDuration := func(text string) string { | |
| 97 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU
RATION]") | |
| 98 } | |
| 99 | |
| 100 Convey("Testing basic rendering.", t, func() { | |
| 101 // Load all the bundles. | |
| 102 c := context.Background() | |
| 103 c = memory.Use(c) | |
| 104 c = settings.WithRequest(c, &http.Request{URL: &url.URL{Path: "/
foobar"}}) | |
| 105 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | |
| 106 a := auth.Authenticator{fakeOAuthMethod{"some_client_id"}} | |
| 107 c = auth.SetAuthenticator(c, a) | |
| 108 c = luciSettings.Use(c, luciSettings.New(&luciSettings.MemorySto
rage{Expiration: time.Second})) | |
| 109 err := luciSettings.Set(c, "analytics", &analyticsSettings{"UA-1
2345-01"}, "", "") | |
| 110 So(err, ShouldBeNil) | |
| 111 for _, nb := range settings.GetTemplateBundles() { | |
| 112 Convey(fmt.Sprintf("Testing theme %q", nb.Name), func()
{ | |
| 113 err := nb.Bundle.EnsureLoaded(c) | |
| 114 So(err, ShouldBeNil) | |
| 115 for _, h := range allHandlers { | |
| 116 hName := reflect.TypeOf(h).String() | |
| 117 Convey(fmt.Sprintf("Testing handler %q",
hName), func() { | |
| 118 for _, b := range h.TestData() { | |
| 119 Convey(fmt.Sprintf("Test
ing: %q", b.Description), func() { | |
| 120 args := b.Data | |
| 121 // This is not a
path, but a file key, should always be "/". | |
| 122 tmplName := fmt.
Sprintf( | |
| 123 "pages/%
s", h.GetTemplateName(*nb.Theme)) | |
| 124 buf, err := nb.B
undle.Render(c, tmplName, args) | |
| 125 So(err, ShouldBe
Nil) | |
| 126 fname := fmt.Spr
intf( | |
| 127 "%s-%s-%
s.html", nb.Name, hName, b.Description) | |
| 128 if *generate { | |
| 129 mustWrit
e(fname, buf) | |
| 130 } else { | |
| 131 localBuf
, err := load(fname) | |
| 132 So(err,
ShouldBeNil) | |
| 133 So(fixZe
roDuration(string(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) | |
| 134 } | |
| 135 }) | |
| 136 } | |
| 137 }) | |
| 138 } | |
| 139 }) | |
| 140 } | |
| 141 }) | |
| 142 } | |
| OLD | NEW |