Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): add new tests for errors.go #109

Merged
merged 18 commits into from
Jul 28, 2021
Merged
43 changes: 21 additions & 22 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
- master
- /refs/heads/master

defaults:
run:
working-directory: ./src/github.com/SSHcom/privx-sdk-go

jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -17,48 +21,43 @@ jobs:
- name: golang
uses: actions/setup-go@v2
with:
go-version: 1.14

- name: fix GOPATH
run: |
echo "GOPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
echo "$GITHUB_WORKSPACE/bin" >> $GITHUB_PATH
shell: bash
go-version: 1.16

- name: checkout
uses: actions/checkout@v2
with:
path: src/github.com/${{ github.repository }}
path: src/github.com/SSHcom/privx-sdk-go

- name: go get tools
- name: Get source dependencies
run: |
go get golang.org/x/lint/golint
go get github.com/mattn/goveralls
go get github.com/lawrencewoodman/roveralls
go mod download -json

- name: go get deps
- name: go get CI tools
run: |
go get -v -t -d github.com/${{ github.repository }}/...
go get github.com/mattn/goveralls
go get github.com/lawrencewoodman/roveralls
env:
GO111MODULE: off

- name: go build
- name: go install staticcheck
run: |
go build -v github.com/${{ github.repository }}/...
go install honnef.co/go/tools/cmd/staticcheck@latest

- name: go vet
run: |
go vet github.com/${{ github.repository }}/...
go vet ./...

- name: golint
- name: staticcheck
run: |
golint -set_exit_status github.com/${{ github.repository }}/...
staticcheck ./...

- name: go test
run: |
go test github.com/${{ github.repository }}/...
go test ./...

- name: coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd $(go env GOPATH)/src/github.com/${{ github.repository }} && roveralls
goveralls -coverprofile=$(go env GOPATH)/src/github.com/${{ github.repository }}/roveralls.coverprofile -service=github
GOPATH=${GITHUB_WORKSPACE} roveralls
GOPATH=${GITHUB_WORKSPACE} goveralls -coverprofile=roveralls.coverprofile -service=github
2 changes: 1 addition & 1 deletion api/userstore/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type APIClient struct {

// LocalUser definition
type LocalUser struct {
ID string `json:"id,omnitempty"`
ID string `json:"id,omitempty"`
Created string `json:"created,omitempty"`
Updated string `json:"updated,omitempty"`
UpdatedBy string `json:"updated_by,omitempty"`
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module github.com/SSHcom/privx-sdk-go

go 1.14
go 1.16

require (
github.com/BurntSushi/toml v0.3.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
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/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
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 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 0 additions & 9 deletions restapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ import (
"github.com/SSHcom/privx-sdk-go/restapi"
)

//
type mockIdP struct{ string }

func (idp mockIdP) AccessToken() (string, error) {
return idp.string, nil
}

var access = restapi.Auth(mockIdP{"Bearer trusted"})

func TestGet(t *testing.T) {
ts := mockStatus()
defer ts.Close()
Expand Down
71 changes: 71 additions & 0 deletions restapi/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright (c) 2021 SSH Communications Security Inc.
//
// All rights reserved.
//

package restapi

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmptyResponseBody(t *testing.T) {
var emptyRespBody []byte

resp, _ := mockResponse()
result := ErrorFromResponse(resp, emptyRespBody)

assert.EqualValues(t, fmt.Errorf("HTTP error: %s", resp.Status), result, "Expected to be equal error status")
}

func TestUnexpectedResponseBody(t *testing.T) {
resp, body := mockResponse()
result := ErrorFromResponse(resp, body)

assert.EqualValues(t, fmt.Errorf("HTTP error: 200 OK (unexpected response body: invalid character '<' looking for beginning of value)"),
result, "Expected to be equal error status")
}

func TestDetailsErrorMessage(t *testing.T) {
errorDetail := []ErrorDetail{{
ErrorCode: "42",
ErrorMessage: "DtlTest",
Property: "Detail",
},
}
body, _ := json.Marshal(ErrorResponse{
ErrorCode: "42",
ErrorMessage: "ErrRspTest",
Property: "ErrRsp",
Details: errorDetail,
})

resp, _ := mockResponse()
result := ErrorFromResponse(resp, body)

assert.EqualValues(t, fmt.Errorf("error: 42, message: ErrRspTest, property: ErrRsp, {error: 42, message: DtlTest, property: Detail}"),
result, "Expected to be equal error status")
}

func mockResponse() (*http.Response, []byte) {
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Test Body!</body></html>")
}

req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
handler(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)

return resp, body
}