94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package kekskurseauth
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewAuthWithConfig(t *testing.T) {
|
|
clientConfig := ClientConfig{ClientID: "abc", ClientSecret: "def"}
|
|
config := AuthConfig{}
|
|
config.TokenEndpoint = "http://localhost/something"
|
|
client, err := NewAuthWithConfig(clientConfig, config)
|
|
assert.Equal(t, nil, err, "should return no error while creating Auth")
|
|
assert.Equal(t, "http://localhost/something", client.authConfig.TokenEndpoint, "should have currect config")
|
|
assert.Equal(t, "abc", client.clientConfig.ClientID, "should have stored currect clientid")
|
|
assert.Equal(t, "def", client.clientConfig.ClientSecret, "should have stored currect client secret")
|
|
}
|
|
|
|
func TestNewAuthWithConfigurationURL(t *testing.T) {
|
|
clientConfig := ClientConfig{ClientID: "abc", ClientSecret: "def"}
|
|
client, err := NewAuthWithConfigurationURL(clientConfig, "http://localhost:8084/openid-configuration")
|
|
assert.Nil(t, err, "should create client without any error")
|
|
assert.Equal(t, "https://auth.keks.cloud/application/o/token/", client.authConfig.TokenEndpoint, "token endpoint should match")
|
|
assert.Equal(t, "abc", client.clientConfig.ClientID, "should have stored currect clientid")
|
|
assert.Equal(t, "def", client.clientConfig.ClientSecret, "should have stored currect client secret")
|
|
}
|
|
|
|
func TestGetAuthorizationUrl(t *testing.T) {
|
|
tts := []struct {
|
|
name string
|
|
config AuthConfig
|
|
redirectURL string
|
|
scops []string
|
|
state string
|
|
exptUrl string
|
|
exptError error
|
|
}{
|
|
{
|
|
name: "error-config-has-no-url",
|
|
exptError: ErrCantGetAuthorizationURL,
|
|
},
|
|
{
|
|
name: "plain-url",
|
|
config: AuthConfig{AuthorizationEndpoint: "http://localhost/something"},
|
|
exptUrl: "http://localhost/something?client_id=abc&response_type=code",
|
|
},
|
|
{
|
|
name: "url-with-redirect-and-state",
|
|
config: AuthConfig{AuthorizationEndpoint: "http://localhost/something"},
|
|
exptUrl: "http://localhost/something?client_id=abc&redirect_uri=https%3A%2F%2Fexample.com&response_type=code&state=randomStateStringWith%C3%A4and%C3%B6ok",
|
|
redirectURL: "https://example.com",
|
|
state: "randomStateStringWithäandöok",
|
|
},
|
|
{
|
|
name: "url-with-scopes",
|
|
config: AuthConfig{AuthorizationEndpoint: "http://localhost/something"},
|
|
scops: []string{"some", "söäüöäüßcopes"},
|
|
exptUrl: "http://localhost/something?client_id=abc&response_type=code&scope=some%2Bs%C3%B6%C3%A4%C3%BC%C3%B6%C3%A4%C3%BC%C3%9Fcopes",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tts {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
config := ClientConfig{ClientID: "abc", ClientSecret: "def", Scope: tt.scops, RedirectURL: tt.redirectURL}
|
|
|
|
client, err := NewAuthWithConfig(config, tt.config)
|
|
assert.Nil(t, err, "should be able to create client without error")
|
|
|
|
url, err := client.GetAuthorizationURL(tt.state)
|
|
if tt.exptError == nil {
|
|
assert.Nil(t, err, "should get link without error")
|
|
} else {
|
|
assert.ErrorIs(t, err, tt.exptError, "should return right error")
|
|
}
|
|
assert.Equal(t, tt.exptUrl, url, "should return right url")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuthenticLogin(t *testing.T) {
|
|
t.Skip("dev test")
|
|
clientConfig := ClientConfig{ClientID: "abc", ClientSecret: "def"}
|
|
client, err := NewAuthWithConfigurationURL(clientConfig, "http://localhost:8084/openid-configuration")
|
|
assert.Nil(t, err, "should be able to create client without error")
|
|
url, err := client.GetAuthorizationURL("")
|
|
assert.Nil(t, err, "should be able to create url without error")
|
|
fmt.Println(url)
|
|
|
|
token, err := client.GetTokenFromCode("e34bc2c7840e4386b17880dd1142c67b")
|
|
assert.Nil(t, err, "should be able to get code without error")
|
|
fmt.Println(token)
|
|
}
|