You've already forked oauth2-proxy
mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-08-08 22:46:33 +02:00
feat: add SourceHut (sr.ht) provider (#2359)
* Add SourceHut (sr.ht) provider * fix changelog entry Signed-off-by: Jan Larwig <jan@larwig.com> --------- Signed-off-by: Jan Larwig <jan@larwig.com> Co-authored-by: Jan Larwig <jan@larwig.com>
This commit is contained in:
77
providers/srht_test.go
Normal file
77
providers/srht_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func testSourceHutProvider(hostname string) *SourceHutProvider {
|
||||
p := NewSourceHutProvider(
|
||||
&ProviderData{
|
||||
ProviderName: "SourceHut",
|
||||
LoginURL: &url.URL{},
|
||||
RedeemURL: &url.URL{},
|
||||
ProfileURL: &url.URL{},
|
||||
ValidateURL: &url.URL{},
|
||||
Scope: ""},
|
||||
)
|
||||
p.ProviderName = "SourceHut"
|
||||
|
||||
if hostname != "" {
|
||||
updateURL(p.Data().LoginURL, hostname)
|
||||
updateURL(p.Data().RedeemURL, hostname)
|
||||
updateURL(p.Data().ProfileURL, hostname)
|
||||
updateURL(p.Data().ValidateURL, hostname)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func testSourceHutBackend(payloads map[string][]string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
index := 0
|
||||
payload, ok := payloads[r.URL.Path]
|
||||
if !ok {
|
||||
w.WriteHeader(404)
|
||||
} else if payload[index] == "" {
|
||||
w.WriteHeader(204)
|
||||
} else {
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(payload[index]))
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestSourceHutProvider_ValidateSessionWithBaseUrl(t *testing.T) {
|
||||
b := testSourceHutBackend(map[string][]string{})
|
||||
defer b.Close()
|
||||
|
||||
bURL, _ := url.Parse(b.URL)
|
||||
p := testSourceHutProvider(bURL.Host)
|
||||
|
||||
session := CreateAuthorizedSession()
|
||||
|
||||
valid := p.ValidateSession(context.Background(), session)
|
||||
assert.False(t, valid)
|
||||
}
|
||||
|
||||
func TestSourceHutProvider_ValidateSessionWithUserEmails(t *testing.T) {
|
||||
b := testSourceHutBackend(map[string][]string{
|
||||
"/query": {`{"data":{"me":{"username":"bitfehler","email":"ch@bitfehler.net"}}}`},
|
||||
"/profile": {`ok`},
|
||||
})
|
||||
defer b.Close()
|
||||
|
||||
bURL, _ := url.Parse(b.URL)
|
||||
p := testSourceHutProvider(bURL.Host)
|
||||
|
||||
session := CreateAuthorizedSession()
|
||||
|
||||
valid := p.ValidateSession(context.Background(), session)
|
||||
assert.True(t, valid)
|
||||
}
|
Reference in New Issue
Block a user