From aaa755eba839b1874f66e948209b1b2841ef8519 Mon Sep 17 00:00:00 2001 From: Florian RUEN Date: Thu, 11 Jan 2024 10:52:20 +0100 Subject: [PATCH] fix: update tests to match type updates --- cmd/np/main.go | 2 +- config/config.go | 47 ++++++++++++++++++------ config/config_test.go | 63 +++++++++++++++++++++++---------- core/router_test.go | 14 ++++---- payments/list_test.go | 6 ++-- payments/minimum_amount_test.go | 2 +- payments/payment_test.go | 2 +- 7 files changed, 96 insertions(+), 40 deletions(-) diff --git a/cmd/np/main.go b/cmd/np/main.go index f55b7c7..3d6786f 100644 --- a/cmd/np/main.go +++ b/cmd/np/main.go @@ -36,7 +36,7 @@ func main() { log.Fatal(err) } defer f.Close() - err = config.Load(f) + err = config.LoadFromFile(f) if err != nil { log.Fatal(err) } diff --git a/config/config.go b/config/config.go index ea13104..5eee9f8 100644 --- a/config/config.go +++ b/config/config.go @@ -9,7 +9,7 @@ import ( "github.com/rotisserie/eris" ) -type credentials struct { +type Credentials struct { APIKey string `json:"apiKey"` IPNSecretKey string `json:"ipnSecretKey"` Login string `json:"login"` @@ -17,33 +17,60 @@ type credentials struct { Server string `json:"server"` } -var conf credentials +var conf Credentials func configErr(err error) error { return eris.Wrap(err, "config") } // Load parses a JSON file to get the required credentials to operate NOWPayment's API. -func Load(r io.Reader) error { - if r == nil { +func Load(c *Credentials) error { + if c == nil { return configErr(errors.New("nil reader")) } - conf = credentials{} - d := json.NewDecoder(r) + conf = *c + // Sanity checks. + if conf.APIKey == "" { + return configErr(errors.New("API key is missing")) + } + if conf.IPNSecretKey == "" { + return configErr(errors.New("IPN secret key is missing")) + } + if conf.Login == "" { + return configErr(errors.New("login info missing")) + } + if conf.Password == "" { + return configErr(errors.New("password info missing")) + } + if conf.Server == "" { + return configErr(errors.New("server URL missing")) + } else { + _, err := url.Parse(conf.Server) + if err != nil { + return configErr(errors.New("server URL parsing")) + } + } + + return nil +} + +// LoadFromFile parses a JSON file to get the required credentials to operate NOWPayment's API. +func LoadFromFile(r io.Reader) error { + if r == nil { + return configErr(errors.New("nil reader")) + } + conf = Credentials{} + d := json.NewDecoder(r) err := d.Decode(&conf) if err != nil { return configErr(err) } - // Sanity checks. if conf.APIKey == "" { return configErr(errors.New("API key is missing")) } - if conf.IPNSecretKey == "" { - return configErr(errors.New("IPN secret key is missing")) - } if conf.Login == "" { return configErr(errors.New("login info missing")) } diff --git a/config/config_test.go b/config/config_test.go index 887b097..f6a4b39 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,11 +6,47 @@ import ( "testing" ) +var validCfg = Credentials{ + Server: "http://some.tld", + Login: "mylogin", + Password: "mypass", + APIKey: "key", + IPNSecretKey: "key", +} + func TestLoad(t *testing.T) { - emptyAPIKeyCfg := `{"server":"http://some.tld","login":"mylogin","password":"mypass"}` - emptyLoginCfg := `{"server":"http://some.tld","apiKey":"key","password":"mypass"}` - emptyPasswordCfg := `{"server":"http://some.tld","login":"mylogin","apiKey":"key"}` - emptyServerCfg := `{"apiKey":"key","login":"mylogin","password":"mypass"}` + emptyAPIKeyCfg := Credentials{Server: "http://some.tld", Login: "mylogin", Password: "mypass", IPNSecretKey: "key"} + emptyLoginCfg := Credentials{Server: "http://some.tld", APIKey: "key", Password: "mypass", IPNSecretKey: "key"} + emptyPasswordCfg := Credentials{Server: "http://some.tld", APIKey: "key", Login: "mylogin", IPNSecretKey: "key"} + emptyServerCfg := Credentials{APIKey: "key", Login: "mylogin", Password: "mypass", IPNSecretKey: "key"} + tests := []struct { + name string + r *Credentials + wantErr bool + }{ + {"nil reader", nil, true}, + {"bad config", &Credentials{}, true}, + {"valid config", &validCfg, false}, + {"empty API key", &emptyAPIKeyCfg, true}, + {"empty login", &emptyLoginCfg, true}, + {"empty password", &emptyPasswordCfg, true}, + {"empty server", &emptyServerCfg, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Load(tt.r); (err != nil) != tt.wantErr { + t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestLoadFromFile(t *testing.T) { + emptyAPIKeyCfg := `{"server":"http://some.tld","login":"mylogin","password":"mypass","ipnSecretKey":"key"}` + emptyLoginCfg := `{"server":"http://some.tld","apiKey":"key","password":"mypass","ipnSecretKey":"key"}` + emptyPasswordCfg := `{"server":"http://some.tld","login":"mylogin","apiKey":"key","ipnSecretKey":"key"}` + emptyServerCfg := `{"apiKey":"key","login":"mylogin","password":"mypass","ipnSecretKey":"key"}` + validCfg := `{"server":"http://some.tld","apiKey":"key","login":"mylogin","password":"mypass","ipnSecretKey":"key"}` tests := []struct { name string r io.Reader @@ -26,24 +62,15 @@ func TestLoad(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := Load(tt.r); (err != nil) != tt.wantErr { + if err := LoadFromFile(tt.r); (err != nil) != tt.wantErr { t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr) } }) } } -var validCfg = ` -{ - "server": "http://some.tld", - "login": "mylogin", - "password": "mypass", - "apiKey": "key" -} -` - func TestLogin(t *testing.T) { - Load(strings.NewReader(validCfg)) + Load(&validCfg) tests := []struct { name string want string @@ -60,7 +87,7 @@ func TestLogin(t *testing.T) { } func TestPassword(t *testing.T) { - Load(strings.NewReader(validCfg)) + Load(&validCfg) tests := []struct { name string want string @@ -77,7 +104,7 @@ func TestPassword(t *testing.T) { } func TestAPIKey(t *testing.T) { - Load(strings.NewReader(validCfg)) + Load(&validCfg) tests := []struct { name string want string @@ -94,7 +121,7 @@ func TestAPIKey(t *testing.T) { } func TestServer(t *testing.T) { - Load(strings.NewReader(validCfg)) + Load(&validCfg) tests := []struct { name string want string diff --git a/core/router_test.go b/core/router_test.go index 87aa157..3d05e15 100644 --- a/core/router_test.go +++ b/core/router_test.go @@ -34,12 +34,14 @@ func newResponseOK(body string) *http.Response { return newResponse(http.StatusOK, body) } -func conf() *strings.Reader { - return strings.NewReader(` -{ - "login":"l","password":"p","apiKey":"key","server":"http://some.tld" -} -`) +func conf() *config.Credentials { + return &config.Credentials{ + Login: "l", + Password: "p", + APIKey: "key", + Server: "http://some.tld", + IPNSecretKey: "ipn", + } } func init() { diff --git a/payments/list_test.go b/payments/list_test.go index ea98cc0..ff34b69 100644 --- a/payments/list_test.go +++ b/payments/list_test.go @@ -38,7 +38,7 @@ func TestList(t *testing.T) { func(ps []*Payment[int64], err error) { assert.NoError(err) if assert.Len(ps, 1) { - assert.Equal("1", ps[0].ID) + assert.Equal(int64(1), ps[0].ID) } }}, {"payment_id as a string", nil, @@ -49,7 +49,7 @@ func TestList(t *testing.T) { case "/v1/auth": return newResponseOK(`{"token":"tok"}`) case "/v1/payment/": - return newResponseOK(`{"data":[{"payment_id":"54321"}]}`) + return newResponseOK(`{"data":[{"payment_id":54321}]}`) default: t.Fatalf("unexpected route call %q", req.URL.Path) } @@ -59,7 +59,7 @@ func TestList(t *testing.T) { func(ps []*Payment[int64], err error) { assert.NoError(err) if assert.Len(ps, 1) { - assert.Equal("54321", ps[0].ID) + assert.Equal(int64(54321), ps[0].ID) } }}, {"api error", nil, diff --git a/payments/minimum_amount_test.go b/payments/minimum_amount_test.go index 4cb8b3f..3bd68bd 100644 --- a/payments/minimum_amount_test.go +++ b/payments/minimum_amount_test.go @@ -29,7 +29,7 @@ func TestMinimumAmount(t *testing.T) { func(c *mocks.HTTPClient) { resp := newResponseOK(`{"currency_from":"eur","currency_to":"btc","min_amount":1.0}`) c.EXPECT().Do(mock.Anything).Run(func(req *http.Request) { - assert.Equal("currency_from=eur¤cy_to=btc", + assert.Equal("currency_from=eur¤cy_to=btc&fiat_equivalent=usd", req.URL.Query().Encode(), "check query parameters") assert.Equal("/v1/min-amount", req.URL.Path, "bad endpoint") }).Return(resp, nil) diff --git a/payments/payment_test.go b/payments/payment_test.go index 69a44dd..a9c59fc 100644 --- a/payments/payment_test.go +++ b/payments/payment_test.go @@ -53,7 +53,7 @@ func TestNew(t *testing.T) { PaymentAmount: PaymentAmount{PriceAmount: 10.0}, }, func(c *mocks.HTTPClient) { - resp := newResponseOK(`{"payment_id":"1234","pay_amount":"3.5"}`) + resp := newResponseOK(`{"payment_id":"1234","pay_amount":3.5}`) c.EXPECT().Do(mock.Anything).Return(resp, nil) }, func(p *Payment[string], err error) { assert.NoError(err)