| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package frontend | 5 package frontend |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "flag" | 8 "flag" |
| 9 "fmt" | 9 "fmt" |
| 10 "io/ioutil" | 10 "io/ioutil" |
| 11 "net/http" | 11 "net/http" |
| 12 "net/url" | 12 "net/url" |
| 13 "path/filepath" | 13 "path/filepath" |
| 14 "regexp" | 14 "regexp" |
| 15 "strings" | 15 "strings" |
| 16 "testing" | 16 "testing" |
| 17 "time" | 17 "time" |
| 18 | 18 |
| 19 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 20 | 20 |
| 21 "github.com/luci/gae/impl/memory" | 21 "github.com/luci/gae/impl/memory" |
| 22 "github.com/luci/luci-go/common/clock/testclock" | 22 "github.com/luci/luci-go/common/clock/testclock" |
| 23 "github.com/luci/luci-go/milo/appengine/buildbot" | 23 "github.com/luci/luci-go/milo/appengine/buildbot" |
| 24 "github.com/luci/luci-go/milo/appengine/common" | 24 "github.com/luci/luci-go/milo/appengine/common" |
| 25 "github.com/luci/luci-go/milo/appengine/swarming" | 25 "github.com/luci/luci-go/milo/appengine/swarming" |
| 26 "github.com/luci/luci-go/server/auth" | 26 "github.com/luci/luci-go/server/auth" |
| 27 "github.com/luci/luci-go/server/auth/authtest" |
| 27 "github.com/luci/luci-go/server/auth/identity" | 28 "github.com/luci/luci-go/server/auth/identity" |
| 28 "github.com/luci/luci-go/server/settings" | 29 "github.com/luci/luci-go/server/settings" |
| 29 "github.com/luci/luci-go/server/templates" | 30 "github.com/luci/luci-go/server/templates" |
| 30 | 31 |
| 31 . "github.com/smartystreets/goconvey/convey" | 32 . "github.com/smartystreets/goconvey/convey" |
| 32 ) | 33 ) |
| 33 | 34 |
| 34 type testPackage struct { | 35 type testPackage struct { |
| 35 Data func() []common.TestBundle | 36 Data func() []common.TestBundle |
| 36 DisplayName string | 37 DisplayName string |
| (...skipping 29 matching lines...) Expand all Loading... |
| 66 // panic. This is fine because this only runs when -generate is passed in, | 67 // panic. This is fine because this only runs when -generate is passed in, |
| 67 // not during tests. | 68 // not during tests. |
| 68 func mustWrite(name string, buf []byte) { | 69 func mustWrite(name string, buf []byte) { |
| 69 filename := expectFileName(name) | 70 filename := expectFileName(name) |
| 70 err := ioutil.WriteFile(filename, buf, 0644) | 71 err := ioutil.WriteFile(filename, buf, 0644) |
| 71 if err != nil { | 72 if err != nil { |
| 72 panic(err) | 73 panic(err) |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| 76 // fakeOAuthMethod implements Method. | |
| 77 type fakeOAuthMethod struct { | |
| 78 clientID string | |
| 79 } | |
| 80 | |
| 81 func (m fakeOAuthMethod) Authenticate(context.Context, *http.Request) (*auth.Use
r, error) { | |
| 82 return &auth.User{ | |
| 83 Identity: identity.Identity("user:abc@example.com"), | |
| 84 Email: "abc@example.com", | |
| 85 ClientID: m.clientID, | |
| 86 }, nil | |
| 87 } | |
| 88 | |
| 89 func (m fakeOAuthMethod) LoginURL(c context.Context, target string) (string, err
or) { | |
| 90 return "https://login.url/?target=" + target, nil | |
| 91 } | |
| 92 | |
| 93 func (m fakeOAuthMethod) LogoutURL(c context.Context, target string) (string, er
ror) { | |
| 94 return "https://logout.url/?target=" + target, nil | |
| 95 } | |
| 96 | |
| 97 type analyticsSettings struct { | 77 type analyticsSettings struct { |
| 98 AnalyticsID string `json:"analytics_id"` | 78 AnalyticsID string `json:"analytics_id"` |
| 99 } | 79 } |
| 100 | 80 |
| 101 func TestPages(t *testing.T) { | 81 func TestPages(t *testing.T) { |
| 102 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) | 82 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) |
| 103 fixZeroDuration := func(text string) string { | 83 fixZeroDuration := func(text string) string { |
| 104 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU
RATION]") | 84 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU
RATION]") |
| 105 } | 85 } |
| 106 | 86 |
| 107 Convey("Testing basic rendering.", t, func() { | 87 Convey("Testing basic rendering.", t, func() { |
| 108 c := context.Background() | 88 c := context.Background() |
| 109 c = memory.Use(c) | 89 c = memory.Use(c) |
| 110 c = common.WithRequest(c, &http.Request{URL: &url.URL{Path: "/fo
obar"}}) | 90 c = common.WithRequest(c, &http.Request{URL: &url.URL{Path: "/fo
obar"}}) |
| 111 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | 91 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) |
| 112 » » a := auth.Authenticator{fakeOAuthMethod{"some_client_id"}} | 92 » » c = auth.WithState(c, &authtest.FakeState{Identity: identity.Ano
nymousIdentity}) |
| 113 » » c = auth.SetAuthenticator(c, a) | |
| 114 c = settings.Use(c, settings.New(&settings.MemoryStorage{Expirat
ion: time.Second})) | 93 c = settings.Use(c, settings.New(&settings.MemoryStorage{Expirat
ion: time.Second})) |
| 115 err := settings.Set(c, "analytics", &analyticsSettings{"UA-12345
-01"}, "", "") | 94 err := settings.Set(c, "analytics", &analyticsSettings{"UA-12345
-01"}, "", "") |
| 116 So(err, ShouldBeNil) | 95 So(err, ShouldBeNil) |
| 117 c = templates.Use(c, common.GetTemplateBundle()) | 96 c = templates.Use(c, common.GetTemplateBundle()) |
| 118 for _, p := range allPackages { | 97 for _, p := range allPackages { |
| 119 Convey(fmt.Sprintf("Testing handler %q", p.DisplayName),
func() { | 98 Convey(fmt.Sprintf("Testing handler %q", p.DisplayName),
func() { |
| 120 for _, b := range p.Data() { | 99 for _, b := range p.Data() { |
| 121 Convey(fmt.Sprintf("Testing: %q", b.Desc
ription), func() { | 100 Convey(fmt.Sprintf("Testing: %q", b.Desc
ription), func() { |
| 122 args := b.Data | 101 args := b.Data |
| 123 // This is not a path, but a fil
e key, should always be "/". | 102 // This is not a path, but a fil
e key, should always be "/". |
| 124 tmplName := "pages/" + p.Templat
eName | 103 tmplName := "pages/" + p.Templat
eName |
| 125 buf, err := templates.Render(c,
tmplName, args) | 104 buf, err := templates.Render(c,
tmplName, args) |
| 126 So(err, ShouldBeNil) | 105 So(err, ShouldBeNil) |
| 127 fname := fmt.Sprintf( | 106 fname := fmt.Sprintf( |
| 128 "%s-%s.html", p.DisplayN
ame, b.Description) | 107 "%s-%s.html", p.DisplayN
ame, b.Description) |
| 129 if *generate { | 108 if *generate { |
| 130 mustWrite(fname, buf) | 109 mustWrite(fname, buf) |
| 131 } else { | 110 } else { |
| 132 localBuf, err := load(fn
ame) | 111 localBuf, err := load(fn
ame) |
| 133 So(err, ShouldBeNil) | 112 So(err, ShouldBeNil) |
| 134 So(fixZeroDuration(strin
g(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) | 113 So(fixZeroDuration(strin
g(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) |
| 135 } | 114 } |
| 136 }) | 115 }) |
| 137 } | 116 } |
| 138 }) | 117 }) |
| 139 } | 118 } |
| 140 }) | 119 }) |
| 141 } | 120 } |
| OLD | NEW |