Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client) Support empty config and tests #8

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions login/noop/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package noop

import (
"context"
"net/http"

"golang.org/x/oauth2"

"github.com/holyhope/digiposte-go-sdk/login"
)

type LoginMethod struct {
Token *oauth2.Token
Cookies []*http.Cookie
}

func (lm *LoginMethod) Login(_ context.Context, _ *login.Credentials) (*oauth2.Token, []*http.Cookie, error) {
return lm.Token, lm.Cookies, nil
}
30 changes: 30 additions & 0 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/oauth2"

login "github.com/holyhope/digiposte-go-sdk/login"
"github.com/holyhope/digiposte-go-sdk/login/chrome"
"github.com/holyhope/digiposte-go-sdk/settings"
)

Expand All @@ -37,8 +38,37 @@ type Config struct {
Credentials *login.Credentials
}

func (c *Config) SetupDefault() error {
if c.APIURL == "" {
c.APIURL = settings.DefaultAPIURL
}

if c.DocumentURL == "" {
c.DocumentURL = settings.DefaultDocumentURL
}

if c.LoginMethod == nil {
method, err := chrome.New()
if err != nil {
return fmt.Errorf("new chrome login method: %w", err)
}

c.LoginMethod = method
}

return nil
}

// NewAuthenticatedClient creates a new Digiposte client with the given credentials.
func NewAuthenticatedClient(ctx context.Context, client *http.Client, config *Config) (*Client, error) {
if config == nil {
config = new(Config)
}

if err := config.SetupDefault(); err != nil {
return nil, fmt.Errorf("setup default config: %w", err)
}

token, cookies, err := config.LoginMethod.Login(ctx, config.Credentials)
if err != nil {
return nil, fmt.Errorf("login: %w", err)
Expand Down
54 changes: 54 additions & 0 deletions v1/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package digiposte_test

import (
"net/http"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"golang.org/x/oauth2"

"github.com/holyhope/digiposte-go-sdk/login/noop"
"github.com/holyhope/digiposte-go-sdk/settings"
"github.com/holyhope/digiposte-go-sdk/v1"
)

var _ = ginkgo.Describe("Client", func() {
ginkgo.Describe("NewClient", func() {
ginkgo.It("Should return a new client", func() {
gomega.Expect(digiposte.NewClient(nil)).ToNot(gomega.BeNil())
})
})

ginkgo.Describe("NewAuthenticatedClient", func() {
ginkgo.It("Should return a new client", func(ctx ginkgo.SpecContext) {
gomega.Expect(digiposte.NewAuthenticatedClient(ctx, http.DefaultClient, &digiposte.Config{
APIURL: "",
DocumentURL: "",
Credentials: nil,
LoginMethod: &noop.LoginMethod{
Token: &oauth2.Token{
AccessToken: "token",
TokenType: "Bearer",
RefreshToken: "refresh",
Expiry: time.Now().Add(time.Minute),
},
Cookies: nil,
},
})).ToNot(gomega.BeNil())
})
})
})

var _ = ginkgo.Describe("Config", func() {
ginkgo.Describe("SetupDefault", func() {
ginkgo.It("Should work", func() {
var config digiposte.Config

gomega.Expect(config.SetupDefault()).To(gomega.Succeed())
gomega.Expect(config.APIURL).To(gomega.Equal(settings.DefaultAPIURL))
gomega.Expect(config.DocumentURL).To(gomega.Equal(settings.DefaultDocumentURL))
gomega.Expect(config.LoginMethod).ToNot(gomega.BeNil())
})
})
})
Loading