-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package middleware_test | |
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
@@ -14,6 +15,7 @@ import ( | |
|
||
"github.com/canonical/jimm/v3/internal/auth" | ||
"github.com/canonical/jimm/v3/internal/dbmodel" | ||
jimm_errors "github.com/canonical/jimm/v3/internal/errors" | ||
"github.com/canonical/jimm/v3/internal/jimmtest" | ||
"github.com/canonical/jimm/v3/internal/jimmtest/mocks" | ||
"github.com/canonical/jimm/v3/internal/middleware" | ||
|
@@ -96,3 +98,71 @@ func TestAuthenticateRebac(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestAuthenticateViaBasicAuth(t *testing.T) { | ||
testUser := "[email protected]" | ||
jt := jimmtest.JIMM{ | ||
LoginService: mocks.LoginService{ | ||
LoginWithSessionToken_: func(ctx context.Context, sessionToken string) (*openfga.User, error) { | ||
if sessionToken != "good" { | ||
return nil, jimm_errors.E(jimm_errors.CodeSessionTokenInvalid) | ||
} | ||
user := dbmodel.Identity{Name: testUser} | ||
return &openfga.User{Identity: &user, JimmAdmin: true}, nil | ||
}, | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
jimmAdmin bool | ||
expectedStatus int | ||
basicAuthPassword string | ||
errorExpected string | ||
}{ | ||
{ | ||
name: "success", | ||
jimmAdmin: true, | ||
expectedStatus: http.StatusOK, | ||
basicAuthPassword: "good", | ||
}, | ||
{ | ||
name: "failure", | ||
expectedStatus: http.StatusUnauthorized, | ||
basicAuthPassword: "bad", | ||
errorExpected: "error authenticating the user", | ||
}, | ||
{ | ||
name: "no basic auth", | ||
expectedStatus: http.StatusUnauthorized, | ||
errorExpected: "authentication missing", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := qt.New(t) | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
w := httptest.NewRecorder() | ||
if tt.basicAuthPassword != "" { | ||
req.SetBasicAuth("", tt.basicAuthPassword) | ||
} | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
user, err := middleware.IdentityFromContext(r.Context()) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(user.Name, qt.Equals, testUser) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
middleware := middleware.AuthenticateWithSessionTokenViaBasicAuth(handler, &jt) | ||
middleware.ServeHTTP(w, req) | ||
c.Assert(w.Code, qt.Equals, tt.expectedStatus) | ||
b := w.Result().Body | ||
defer b.Close() | ||
body, err := io.ReadAll(b) | ||
c.Assert(err, qt.IsNil) | ||
if tt.errorExpected != "" { | ||
c.Assert(string(body), qt.Matches, tt.errorExpected) | ||
} | ||
|
||
}) | ||
} | ||
} |