Skip to content

Commit 2fa4222

Browse files
committed
Add test for basic auth package
1 parent 727f6c7 commit 2fa4222

File tree

5 files changed

+103
-12
lines changed

5 files changed

+103
-12
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Set up Go
14-
uses: actions/setup-go@v5
14+
uses: actions/setup-go@v6
15+
with:
16+
go-version: "1.25"
1517
- name: Checkout repo
16-
uses: actions/checkout@v4
18+
uses: actions/checkout@v5
1719
- name: Lint code
1820
uses: golangci/golangci-lint-action@v8
1921
- name: Run tests

basicauth/basicauth_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package basicauth_test
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/cloudlena/adapters/basicauth"
10+
)
11+
12+
func TestHandler(t *testing.T) {
13+
users := []basicauth.User{
14+
{Username: "user", Password: "password"},
15+
}
16+
realm := "My Realm"
17+
18+
tests := []struct {
19+
name string
20+
username string
21+
password string
22+
expectedStatus int
23+
expectedBody string
24+
}{
25+
{
26+
name: "No credentials",
27+
username: "",
28+
password: "",
29+
expectedStatus: http.StatusUnauthorized,
30+
expectedBody: "Unauthorized\n",
31+
},
32+
{
33+
name: "Invalid credentials",
34+
username: "wronguser",
35+
password: "wrongpass",
36+
expectedStatus: http.StatusUnauthorized,
37+
expectedBody: "Unauthorized\n",
38+
},
39+
{
40+
name: "Valid credentials",
41+
username: "user",
42+
password: "password",
43+
expectedStatus: http.StatusOK,
44+
expectedBody: "OK",
45+
},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
req := httptest.NewRequest(http.MethodGet, "/", nil)
51+
if tt.username != "" || tt.password != "" {
52+
req.SetBasicAuth(tt.username, tt.password)
53+
}
54+
rr := httptest.NewRecorder()
55+
56+
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
_, err := fmt.Fprint(w, "OK")
58+
if err != nil {
59+
t.FailNow()
60+
}
61+
})
62+
63+
handler := basicauth.Handler(realm, users)(nextHandler)
64+
handler.ServeHTTP(rr, req)
65+
66+
if rr.Code != tt.expectedStatus {
67+
t.Errorf("Expected status %d, got %d", tt.expectedStatus, rr.Code)
68+
}
69+
if rr.Body.String() != tt.expectedBody {
70+
t.Errorf("Expected body %q, got %q", tt.expectedBody, rr.Body.String())
71+
}
72+
})
73+
}
74+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/cloudlena/adapters
22

3-
go 1.24.5
3+
go 1.25.1
44

55
require (
6-
github.com/golang-jwt/jwt/v5 v5.2.3
7-
golang.org/x/oauth2 v0.30.0
6+
github.com/golang-jwt/jwt/v5 v5.3.0
7+
golang.org/x/oauth2 v0.32.0
88
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/golang-jwt/jwt/v5 v5.2.3 h1:kkGXqQOBSDDWRhWNXTFpqGSCMyh/PLnqUvMGJPDJDs0=
2-
github.com/golang-jwt/jwt/v5 v5.2.3/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3-
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
4-
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
1+
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
2+
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
3+
golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
4+
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=

oauth2/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This allows you to use multiple IDPs at the same time and federate your sessions
1010
package main
1111

1212
import (
13+
"encoding/json"
1314
"errors"
1415
"fmt"
1516
"log"
@@ -18,6 +19,7 @@ import (
1819
"time"
1920

2021
"github.com/cloudlena/adapters/oauth2"
22+
jwt "github.com/golang-jwt/jwt/v5"
2123
oa2 "golang.org/x/oauth2"
2224
"golang.org/x/oauth2/facebook"
2325
)
@@ -31,19 +33,32 @@ func IndexHandler() http.Handler {
3133

3234
// parseFacebookToken creates the private claims for an internal JWT from a Facebook OAuth2 token.
3335
func parseFacebookToken(tok *oauth2.Token) (jwt.MapClaims, error) {
36+
var claims struct {
37+
ID string `json:"id"`
38+
Email string `json:"email"`
39+
}
40+
3441
meURL := "https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token=" + url.QueryEscape(tok.AccessToken)
3542
res, err := http.Get(meURL)
3643
if err != nil {
37-
return jwt.MapClaims{}, err
44+
return nil, err
3845
}
3946
defer res.Body.Close()
4047

4148
// Check if request was successful
4249
if res.StatusCode != http.StatusOK {
43-
return jwt.MapClaims{}, errors.New("invalid token")
50+
return nil, errors.New("invalid token response")
51+
}
52+
53+
err = json.NewDecoder(res.Body).Decode(&claims)
54+
if err != nil {
55+
return nil, err
4456
}
4557

46-
return jwt.MapClaims{}, nil
58+
return jwt.MapClaims{
59+
"id": claims.ID,
60+
"email": claims.Email,
61+
}, nil
4762
}
4863

4964
func main() {

0 commit comments

Comments
 (0)