Skip to content

Commit

Permalink
dashboard/app: test existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
tarasmadan committed Oct 15, 2024
1 parent 084d817 commit a845537
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
29 changes: 16 additions & 13 deletions dashboard/app/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func checkAccessLevel(c context.Context, r *http.Request, level AccessLevel) err
return ErrAccess
}

// AuthDomain is broken in AppEngine tests.
var isBrokenAuthDomainInTest = false

func emailInAuthDomains(email string, authDomains []string) bool {
for _, authDomain := range authDomains {
if strings.HasSuffix(email, authDomain) {
Expand All @@ -59,7 +56,7 @@ func emailInAuthDomains(email string, authDomains []string) bool {
return false
}

func currentUser(c context.Context, r *http.Request) *user.User {
func currentUser(c context.Context) *user.User {
u := user.Current(c)
if u != nil {
return u
Expand All @@ -78,23 +75,29 @@ func currentUser(c context.Context, r *http.Request) *user.User {
// OAuth2 token is expected to be present in "Authorization" header.
// Example: "Authorization: Bearer $(gcloud auth print-access-token)".
func accessLevel(c context.Context, r *http.Request) AccessLevel {
if user.IsAdmin(c) {
switch r.FormValue("access") {
return userAccessLevel(currentUser(c), r.FormValue("access"), getConfig(c))

}

Check failure on line 80 in dashboard/app/access.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofmt`-ed with `-s` (gofmt)
// AuthDomain is broken in AppEngine tests.
var trustedAuthDomain = "gmail.com"

func userAccessLevel(u *user.User, wantAccess string, config *GlobalConfig) AccessLevel {
if u == nil {
return AccessPublic
}
if u.Admin {
switch wantAccess {
case "public":
return AccessPublic
case "user":
return AccessUser
}
return AccessAdmin
}
u := currentUser(c, r)
if u == nil ||
// Devappserver does not pass AuthDomain.
u.AuthDomain != "gmail.com" && !isBrokenAuthDomainInTest ||
!emailInAuthDomains(u.Email, getConfig(c).AuthDomains) {
return AccessPublic
if u.AuthDomain == trustedAuthDomain && emailInAuthDomains(u.Email, config.AuthDomains) {
return AccessUser
}
return AccessUser
return AccessPublic
}

func checkTextAccess(c context.Context, r *http.Request, tag string, id int64) (*Bug, *Crash, error) {
Expand Down
4 changes: 4 additions & 0 deletions dashboard/app/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,7 @@ func TestAccess(t *testing.T) {
}
}
}

func TestUserAccessLevel(t *testing.T) {
assert.Equal(t, AccessAdmin, userAccessLevel(&user.User{Admin: true}, "", nil))
}
2 changes: 1 addition & 1 deletion dashboard/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
os.Setenv("GAE_MODULE_VERSION", "1")
os.Setenv("GAE_MINOR_VERSION", "1")

isBrokenAuthDomainInTest = true
trustedAuthDomain = "" // Devappserver environment value is "", prod value is "gmail.com".
obsoleteWhatWontBeFixBisected = true
notifyAboutUnsuccessfulBisections = true
ensureConfigImmutability = true
Expand Down

0 comments on commit a845537

Please sign in to comment.