Skip to content

Commit

Permalink
fix: update tests to match type updates
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRuen committed Jan 11, 2024
1 parent 5698bb6 commit aaa755e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/np/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
47 changes: 37 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,68 @@ import (
"github.com/rotisserie/eris"
)

type credentials struct {
type Credentials struct {
APIKey string `json:"apiKey"`
IPNSecretKey string `json:"ipnSecretKey"`
Login string `json:"login"`
Password string `json:"password"`
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"))
}
Expand Down
63 changes: 45 additions & 18 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions core/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions payments/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion payments/minimum_amount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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&currency_to=btc",
assert.Equal("currency_from=eur&currency_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)
Expand Down
2 changes: 1 addition & 1 deletion payments/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aaa755e

Please sign in to comment.