Skip to content

Commit

Permalink
Add tests (#4)
Browse files Browse the repository at this point in the history
* set http client

* set http protocol by default

* set default host & protocol

* add request handler

* add req handler mocks

* add `TestGetProfileStatus`

* add some tests

* add `TestSetProfileStatus`

* add `TestGetOwnContact`

* add `TestSetProfileStatusUnsuccess`

* add `TestCheckClientConnection`

* add `TestUseVoucher`

* add tests

* add `TestGetBalance`

* Update client_test.go

* add `TestSetWebSocket`

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* do not check connection in method

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* check amount and address is set

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Update client_test.go

* Create request_test.go

* Create log_test.go

* add new conn error variants
  • Loading branch information
Sagleft authored Dec 26, 2022
1 parent fbc616c commit 40e4d9a
Show file tree
Hide file tree
Showing 16 changed files with 921 additions and 77 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: generate-mocks
generate-mocks:
mockgen -package=bots -source=./v2/internal/reqhandler/reqhandler.go > ./v2/internal/mocks/mock_reqhandler.go
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var connBrokenErrorInfo = []string{
"client disconected",
"connection refused",
"EOF",
"connection was forcibly closed by the remote host",
"invalid connection",
}

// CheckErrorConnBroken - check the text of the request error, determining whether the client must be restarted
Expand Down
1 change: 1 addition & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/beefsack/go-rate v0.0.0-20220214233405-116f4ca011a0
github.com/ctengiz/evtwebsocket v0.0.0-20180717104640-fc3583982591
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.8.1
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df
)
Expand Down
25 changes: 25 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/ctengiz/evtwebsocket v0.0.0-20180717104640-fc3583982591/go.mod h1:lpk
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -14,8 +16,31 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df h1:SCh+kVSG+MN/pU/l0/3ehkc3i9T+G6AQS6jEoxM4ddY=
Expand Down
49 changes: 49 additions & 0 deletions v2/internal/mocks/mock_reqhandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions v2/internal/reqhandler/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package reqhandler

import "net/http"

func closeRequest(resp *http.Response) {
if resp != nil {
resp.Body.Close()
}
}
45 changes: 45 additions & 0 deletions v2/internal/reqhandler/reqhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package reqhandler

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"time"
)

type RequestHandler interface {
Send(reqType, URL string, data []byte) ([]byte, error)
}

type defaultHandler struct {
client *http.Client
}

func NewDefaultHandler(timeout time.Duration) RequestHandler {
return &defaultHandler{
client: &http.Client{Timeout: timeout},
}
}

func (h *defaultHandler) Send(reqType, URL string, data []byte) ([]byte, error) {

req, err := http.NewRequest(reqType, URL, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")

resp, err := h.client.Do(req)
defer closeRequest(resp)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}

return body, nil
}
31 changes: 27 additions & 4 deletions v2/internal/utopia/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@ import (
"strconv"
"time"

"github.com/Sagleft/utopialib-go/v2/internal/reqhandler"
"github.com/Sagleft/utopialib-go/v2/pkg/consts"
"github.com/Sagleft/utopialib-go/v2/pkg/structs"
"github.com/beefsack/go-rate"
)

func NewUtopiaClient(data Config) *UtopiaClient {
var timeoutDuration time.Duration
if data.RequestTimeoutSeconds > 0 {
timeoutDuration = time.Duration(data.RequestTimeoutSeconds) * time.Second
}

if data.Host == "" {
data.Host = defaultHost
}

if data.Protocol == "" {
data.Protocol = defaultProtocol
}

return &UtopiaClient{
data: data,
limiters: getRateLimiters(),
reqHandler: reqhandler.NewDefaultHandler(timeoutDuration),
data: data,
limiters: getRateLimiters(),
}
}

Expand Down Expand Up @@ -58,7 +73,7 @@ func (c *UtopiaClient) SetProfileStatus(status string, mood string) error {
return err
}
if !result {
return errors.New("failed to set profile status")
return ErrorSetProfileStatus
}
return nil
}
Expand Down Expand Up @@ -152,7 +167,7 @@ func (c *UtopiaClient) SetWebSocketState(task structs.SetWsStateTask) error {
return err
}
if result == "" {
return errors.New("failed to set websocker state")
return errors.New("failed to set websocket state: result is empty")
}
return nil
}
Expand Down Expand Up @@ -340,6 +355,14 @@ func (c *UtopiaClient) GetChannelMessages(
}

func (c *UtopiaClient) SendPayment(task structs.SendPaymentTask) (string, error) {
if task.Amount == 0 {
return "", errors.New("amount is not set")
}

if task.To == "" {
return "", errors.New("destination address (`to`) is not set")
}

if task.Comment != "" && len(task.Comment) > maxCharactersInPaymentComment {
return "", fmt.Errorf("comment max length is %v characters", maxCharactersInPaymentComment)
}
Expand Down
Loading

0 comments on commit 40e4d9a

Please sign in to comment.