Files
kubesphere/pkg/apiserver/authentication/identityprovider/github/github_test.go
Wenhao Zhou 62427cda32 Move struct DynamicOptions to package pkg/server (#5625)
* move struct DynamicOptions to package pkg/server/dynamic_options.go

Signed-off-by: wenhaozhou <wenhaozhou@yunify.com>

* update test types

Signed-off-by: wenhaozhou <wenhaozhou@yunify.com>

---------

Signed-off-by: wenhaozhou <wenhaozhou@yunify.com>
2023-04-07 11:33:36 +08:00

167 lines
5.1 KiB
Go

/*
Copyright 2020 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package github
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"kubesphere.io/kubesphere/pkg/server/options"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
)
var githubServer *httptest.Server
func TestGithub(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GitHub Identity Provider Suite")
}
var _ = BeforeSuite(func(done Done) {
githubServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}
switch r.RequestURI {
case "/login/oauth/access_token":
data = map[string]interface{}{
"access_token": "e72e16c7e42f292c6912e7710c838347ae178b4a",
"scope": "user,repo,gist",
"token_type": "bearer",
}
case "/user":
data = map[string]interface{}{
"login": "test",
"email": "test@kubesphere.io",
}
default:
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("not implemented"))
return
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}))
close(done)
}, 60)
var _ = AfterSuite(func() {
By("tearing down the test environment")
gexec.KillAndWait(5 * time.Second)
githubServer.Close()
})
var _ = Describe("GitHub", func() {
Context("GitHub", func() {
var (
provider identityprovider.OAuthProvider
err error
)
It("should configure successfully", func() {
configYAML := `
clientID: de6ff8bed0304e487b6e
clientSecret: 2b70536f79ec8d2939863509d05e2a71c268b9af
redirectURL: "https://ks-console.kubesphere-system.svc/oauth/redirect/github"
scopes:
- user
`
config := mustUnmarshalYAML(configYAML)
factory := ldapProviderFactory{}
provider, err = factory.Create(config)
Expect(err).Should(BeNil())
expected := &github{
ClientID: "de6ff8bed0304e487b6e",
ClientSecret: "2b70536f79ec8d2939863509d05e2a71c268b9af",
Endpoint: endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
UserInfoURL: userInfoURL,
},
RedirectURL: "https://ks-console.kubesphere-system.svc/oauth/redirect/github",
Scopes: []string{"user"},
Config: &oauth2.Config{
ClientID: "de6ff8bed0304e487b6e",
ClientSecret: "2b70536f79ec8d2939863509d05e2a71c268b9af",
Endpoint: oauth2.Endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
},
RedirectURL: "https://ks-console.kubesphere-system.svc/oauth/redirect/github",
Scopes: []string{"user"},
},
}
Expect(provider).Should(Equal(expected))
})
It("should configure successfully", func() {
config := options.DynamicOptions{
"clientID": "de6ff8bed0304e487b6e",
"clientSecret": "2b70536f79ec8d2939863509d05e2a71c268b9af",
"redirectURL": "https://ks-console.kubesphere-system.svc/oauth/redirect/github",
"insecureSkipVerify": true,
"endpoint": options.DynamicOptions{
"authURL": fmt.Sprintf("%s/login/oauth/authorize", githubServer.URL),
"tokenURL": fmt.Sprintf("%s/login/oauth/access_token", githubServer.URL),
"userInfoURL": fmt.Sprintf("%s/user", githubServer.URL),
},
}
factory := ldapProviderFactory{}
provider, err = factory.Create(config)
Expect(err).Should(BeNil())
expected := options.DynamicOptions{
"clientID": "de6ff8bed0304e487b6e",
"clientSecret": "2b70536f79ec8d2939863509d05e2a71c268b9af",
"redirectURL": "https://ks-console.kubesphere-system.svc/oauth/redirect/github",
"insecureSkipVerify": true,
"endpoint": options.DynamicOptions{
"authURL": fmt.Sprintf("%s/login/oauth/authorize", githubServer.URL),
"tokenURL": fmt.Sprintf("%s/login/oauth/access_token", githubServer.URL),
"userInfoURL": fmt.Sprintf("%s/user", githubServer.URL),
},
}
Expect(config).Should(Equal(expected))
})
It("should login successfully", func() {
url, _ := url.Parse("https://ks-console.kubesphere-system.svc/oauth/redirect/test?code=00000")
req := &http.Request{URL: url}
identity, err := provider.IdentityExchangeCallback(req)
Expect(err).Should(BeNil())
Expect(identity.GetUserID()).Should(Equal("test"))
Expect(identity.GetUsername()).Should(Equal("test"))
Expect(identity.GetEmail()).Should(Equal("test@kubesphere.io"))
})
})
})
func mustUnmarshalYAML(data string) options.DynamicOptions {
var dynamicOptions options.DynamicOptions
_ = yaml.Unmarshal([]byte(data), &dynamicOptions)
return dynamicOptions
}