Skip to content

Commit 5af74fb

Browse files
committed
Fixup some unit tests
1 parent dc45be7 commit 5af74fb

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

cli/create_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,11 @@ func TestCreateWithGitAuth(t *testing.T) {
767767

768768
client := coderdtest.New(t, &coderdtest.Options{
769769
ExternalAuthConfigs: []*externalauth.Config{{
770-
OAuth2Config: &testutil.OAuth2Config{},
771-
ID: "github",
772-
Regex: regexp.MustCompile(`github\.com`),
773-
Type: codersdk.EnhancedExternalAuthProviderGitHub.String(),
774-
DisplayName: "GitHub",
770+
InstrumentedOAuth2Config: &testutil.OAuth2Config{},
771+
ID: "github",
772+
Regex: regexp.MustCompile(`github\.com`),
773+
Type: codersdk.EnhancedExternalAuthProviderGitHub.String(),
774+
DisplayName: "GitHub",
775775
}},
776776
IncludeProvisionerDaemon: true,
777777
})

coderd/coderdtest/oidctest/idp.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/go-jose/go-jose/v3"
2525
"github.com/golang-jwt/jwt/v4"
2626
"github.com/google/uuid"
27+
"github.com/prometheus/client_golang/prometheus"
2728
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/require"
2930
"golang.org/x/oauth2"
@@ -33,6 +34,7 @@ import (
3334
"cdr.dev/slog/sloggers/slogtest"
3435
"github.com/coder/coder/v2/coderd"
3536
"github.com/coder/coder/v2/coderd/externalauth"
37+
"github.com/coder/coder/v2/coderd/promoauth"
3638
"github.com/coder/coder/v2/coderd/util/syncmap"
3739
"github.com/coder/coder/v2/codersdk"
3840
)
@@ -943,9 +945,10 @@ func (f *FakeIDP) ExternalAuthConfig(t testing.TB, id string, custom *ExternalAu
943945
handle(email, rw, r)
944946
}
945947
}
948+
instrumentF := promoauth.NewFactory(prometheus.NewRegistry())
946949
cfg := &externalauth.Config{
947-
OAuth2Config: f.OIDCConfig(t, nil),
948-
ID: id,
950+
InstrumentedOAuth2Config: instrumentF.New(f.clientID, f.OIDCConfig(t, nil)),
951+
ID: id,
949952
// No defaults for these fields by omitting the type
950953
Type: "",
951954
DisplayIcon: f.WellknownConfig().UserInfoURL,
@@ -959,7 +962,10 @@ func (f *FakeIDP) ExternalAuthConfig(t testing.TB, id string, custom *ExternalAu
959962
return cfg
960963
}
961964

962-
func (f *FakeIDP) OAuthConfig(scopes ...string) *oauth2.Config {
965+
// OIDCConfig returns the OIDC config to use for Coderd.
966+
func (f *FakeIDP) OIDCConfig(t testing.TB, scopes []string, opts ...func(cfg *coderd.OIDCConfig)) *coderd.OIDCConfig {
967+
t.Helper()
968+
963969
if len(scopes) == 0 {
964970
scopes = []string{"openid", "email", "profile"}
965971
}
@@ -976,15 +982,6 @@ func (f *FakeIDP) OAuthConfig(scopes ...string) *oauth2.Config {
976982
RedirectURL: "https://redirect.com",
977983
Scopes: scopes,
978984
}
979-
f.cfg = oauthCfg
980-
return oauthCfg
981-
}
982-
983-
// OIDCConfig returns the OIDC config to use for Coderd.
984-
func (f *FakeIDP) OIDCConfig(t testing.TB, scopes []string, opts ...func(cfg *coderd.OIDCConfig)) *coderd.OIDCConfig {
985-
t.Helper()
986-
987-
oauthCfg := f.OAuthConfig(scopes...)
988985

989986
ctx := oidc.ClientContext(context.Background(), f.HTTPClient(nil))
990987
p, err := oidc.NewProvider(ctx, f.provider.Issuer)
@@ -1013,6 +1010,7 @@ func (f *FakeIDP) OIDCConfig(t testing.TB, scopes []string, opts ...func(cfg *co
10131010
opt(cfg)
10141011
}
10151012

1013+
f.cfg = oauthCfg
10161014
return cfg
10171015
}
10181016

coderd/promoauth/oauth2.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ func (i *instrumentedTripper) RoundTrip(r *http.Request) (*http.Response, error)
155155
"source": i.source,
156156
"status_code": fmt.Sprintf("%d", statusCode),
157157
}).Inc()
158-
158+
if err == nil {
159+
fmt.Println(map[string]string{
160+
"limit": resp.Header.Get("x-ratelimit-limit"),
161+
"remain": resp.Header.Get("x-ratelimit-remaining"),
162+
"used": resp.Header.Get("x-ratelimit-used"),
163+
"reset": resp.Header.Get("x-ratelimit-reset"),
164+
"resource": resp.Header.Get("x-ratelimit-resource"),
165+
})
166+
}
159167
return resp, err
160168
}

coderd/promoauth/oauth2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestMaintainDefault(t *testing.T) {
2525
}
2626

2727
factory := promoauth.NewFactory(reg)
28-
cfg := factory.New("test", idp.OAuthConfig())
28+
cfg := factory.New("test", idp.OIDCConfig(t, []string{}))
2929

3030
// 0 Requests before we start
3131
require.Equal(t, count(), 0)

testutil/oauth2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testutil
22

33
import (
44
"context"
5+
"net/http"
56
"net/url"
67
"time"
78

@@ -13,6 +14,10 @@ type OAuth2Config struct {
1314
TokenSourceFunc OAuth2TokenSource
1415
}
1516

17+
func (*OAuth2Config) Do(_ context.Context, _ string, req *http.Request) (*http.Response, error) {
18+
return http.DefaultClient.Do(req)
19+
}
20+
1621
func (*OAuth2Config) AuthCodeURL(state string, _ ...oauth2.AuthCodeOption) string {
1722
return "/?state=" + url.QueryEscape(state)
1823
}

0 commit comments

Comments
 (0)