Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imbue committed Nov 8, 2022
1 parent 38ad86c commit 4ddf134
Show file tree
Hide file tree
Showing 21 changed files with 923 additions and 106 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
This library provides convenient access to the eWarehousing Solutions API from applications written in the Go
language.


## Documentation

https://api.docs.ewarehousing-solutions.com/


## Installation

```
Expand All @@ -19,13 +17,20 @@ go get -u github.com/ewarehousing-solutions/ewhs-api-go

- Go 19+


## Usage

```go
...
config := ewhs.NewConfig("username", "password", "wms_code", "customer_code")
client, err := ewhs.NewClient(nil, config)
if err != nil {
log.Fatal(err)
}
```

## Support

[www.ewarehousing-solutions.nl](https://ewarehousing-solutions.nl/)[email protected]

## Credits

# Support
[www.ewarehousing-solutions.nl](https://ewarehousing-solutions.nl/)[email protected]
This package is heavily based on [https://github.com/VictorAvelar/mollie-api-go](VictorAvelar/mollie-api-go).
17 changes: 9 additions & 8 deletions ewhs/articles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ewhs

import (
"context"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -35,8 +36,8 @@ type ArticleListOptions struct {
Direction string `url:"direction,omitempty"`
}

func (as *ArticlesService) List(opts *ArticleListOptions) (list *[]Article, res *Response, err error) {
res, err = as.client.get("wms/articles/", opts)
func (as *ArticlesService) List(ctx context.Context, opts *ArticleListOptions) (list *[]Article, res *Response, err error) {
res, err = as.client.get(ctx, "wms/articles/", opts)
if err != nil {
return
}
Expand All @@ -48,8 +49,8 @@ func (as *ArticlesService) List(opts *ArticleListOptions) (list *[]Article, res
return
}

func (as *ArticlesService) Get(articleID string) (article *Article, res *Response, err error) {
res, err = as.client.get(fmt.Sprintf("wms/articles/%s/", articleID), nil)
func (as *ArticlesService) Get(ctx context.Context, articleID string) (article *Article, res *Response, err error) {
res, err = as.client.get(ctx, fmt.Sprintf("wms/articles/%s/", articleID), nil)
if err != nil {
return
}
Expand All @@ -61,8 +62,8 @@ func (as *ArticlesService) Get(articleID string) (article *Article, res *Respons
return
}

func (as *ArticlesService) Create(art Article) (article *Article, res *Response, err error) {
res, err = as.client.post("wms/articles/", art, nil)
func (as *ArticlesService) Create(ctx context.Context, art Article) (article *Article, res *Response, err error) {
res, err = as.client.post(ctx, "wms/articles/", art, nil)

if err != nil {
return
Expand All @@ -75,8 +76,8 @@ func (as *ArticlesService) Create(art Article) (article *Article, res *Response,
return
}

func (as *ArticlesService) Update(articleID string, art Article) (article *Article, res *Response, err error) {
res, err = as.client.patch(fmt.Sprintf("wms/articles/%s/", articleID), art, nil)
func (as *ArticlesService) Update(ctx context.Context, articleID string, art Article) (article *Article, res *Response, err error) {
res, err = as.client.patch(ctx, fmt.Sprintf("wms/articles/%s/", articleID), art, nil)

if err != nil {
return
Expand Down
8 changes: 8 additions & 0 deletions ewhs/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ewhs

type AuthToken struct {
Token string `json:"token,omitempty"`
Iat int `json:"iat,omitempty"`
Exp int `json:"exp,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
}
77 changes: 77 additions & 0 deletions ewhs/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ewhs

import (
"context"
"fmt"
"github.com/ewarehousing-solutions/ewhs-api-go/test/testdata"
"github.com/stretchr/testify/suite"
"net/http"
"testing"
)

type authServiceSuite struct{ suite.Suite }

func (as *authServiceSuite) TestAuthService_Authorize() {
type args struct {
ctx context.Context
}
cases := []struct {
name string
args args
wantErr bool
err error
pre func()
handler http.HandlerFunc
}{
{
"authorize works as expected.",
args{
context.Background(),
},
false,
nil,
noPre,
func(w http.ResponseWriter, r *http.Request) {
testHeader(as.T(), r, CustomerCodeHeader, "test_customer")
testHeader(as.T(), r, WmsCodeHeader, "test_wms")
testMethod(as.T(), r, "POST")

w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(testdata.CreateAuthTokenResponse))
},
},
{
"wrong login credentials gives an error.",
args{
context.Background(),
},
true,
fmt.Errorf("401 - 401 Unauthorized"),
noPre,
unauthorizedHandler,
},
}

for _, c := range cases {
setup()
defer teardown()

as.T().Run(c.name, func(t *testing.T) {
c.pre()
tMux.HandleFunc("/wms/auth/login/", c.handler)

_, err := tClient.authorize(c.args.ctx)
if c.wantErr {
as.NotNil(err)
as.EqualError(err, c.err.Error())
} else {
as.Nil(err)
as.NotNil(tClient.authToken)
}
})
}
}

func TestAuthService(t *testing.T) {
suite.Run(t, new(authServiceSuite))
}
13 changes: 11 additions & 2 deletions ewhs/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package ewhs

type Config struct {
Username string
Password string
WmsCode string
CustomerCode string
}

func NewConfig() *Config {
return &Config{}
func NewConfig(username string, password string, wmsCode string, customerCode string) *Config {
return &Config{
Username: username,
Password: password,
WmsCode: wmsCode,
CustomerCode: customerCode,
}
}
19 changes: 19 additions & 0 deletions ewhs/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ewhs

import "fmt"

// BaseError contains the general error structure
// returned by mollie.
type BaseError struct {
Status int `json:"status,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
}

// Error interface compliance.
func (be *BaseError) Error() string {
//str := fmt.Sprintf("%d - %s: %s", be.Status, be.Title, be.Detail)
str := fmt.Sprintf("%d - %s", be.Status, be.Title)

return str
}
Loading

0 comments on commit 4ddf134

Please sign in to comment.