Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Jul 20, 2024
1 parent ee43f5c commit 1c1418b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func CreateSignature(
parts := []string{fmt.Sprint(timestamp), httpMethod, "/v2", relativePath}
if len(body) > 0 {
parts = append(parts, string(body))

}

hash := hmac.New(sha256.New, []byte(apiSecret))
hash.Write([]byte(strings.Join(parts, "")))
return hex.EncodeToString(hash.Sum(nil))
Expand Down
19 changes: 19 additions & 0 deletions internal/crypto/crypto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package crypto

import (
"github.com/larscom/bitvavo-go/internal/test"
"testing"
)

func TestCreateSignature(t *testing.T) {
body := "{\"market\":\"ETH-EUR\",\"amount\":1.5,\"price\":2500.5 }"
timestamp := int64(1721452468484)
sig := CreateSignature("GET", "/test", []byte(body), timestamp, "API_SECRET")
test.AssertEqual(t, "d922f806412a560232d5326d95c389893432325f0e89f303f8ed5c9c04cc242b", sig)
}

func TestCreateSignatureNoBody(t *testing.T) {
timestamp := int64(1721452468484)
sig := CreateSignature("GET", "/test", nil, timestamp, "API_SECRET")
test.AssertEqual(t, "dce7f0d49d559d6012733af234fa2bdef5a8492842726405e5c0b514f9bf1f55", sig)
}
4 changes: 2 additions & 2 deletions pkg/bitvavo/util_test.go → internal/test/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bitvavo
package test

import "testing"

func assert[T comparable](t *testing.T, expected T, actual T) {
func AssertEqual[T comparable](t *testing.T, expected T, actual T) {
if expected != actual {
t.Errorf("\nexpected: %v\nactual: %v\n", expected, actual)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/bitvavo/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitvavo

import (
"fmt"
"github.com/larscom/bitvavo-go/internal/test"
"testing"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestMaxOrderNewMarshaller(t *testing.T) {

fmt.Printf("%q", actual)

assert(t, expected, actual)
test.AssertEqual(t, expected, actual)
}

func TestMinOrderNewMarshaller(t *testing.T) {
Expand All @@ -55,7 +56,7 @@ func TestMinOrderNewMarshaller(t *testing.T) {

fmt.Printf("%q", actual)

assert(t, expected, actual)
test.AssertEqual(t, expected, actual)
}

func TestMaxOrderUpdateMarshaller(t *testing.T) {
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestMaxOrderUpdateMarshaller(t *testing.T) {

fmt.Printf("%q", actual)

assert(t, expected, actual)
test.AssertEqual(t, expected, actual)
}

func TestMinOrderUpdateMarshaller(t *testing.T) {
Expand All @@ -102,5 +103,5 @@ func TestMinOrderUpdateMarshaller(t *testing.T) {

fmt.Printf("%q", actual)

assert(t, expected, actual)
test.AssertEqual(t, expected, actual)
}

0 comments on commit 1c1418b

Please sign in to comment.