diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 311e6e8..9bedda3 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 @@ -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 diff --git a/api/userstore/model.go b/api/userstore/model.go index 3fd42bc..4cc1114 100644 --- a/api/userstore/model.go +++ b/api/userstore/model.go @@ -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"` diff --git a/go.mod b/go.mod index cd1ed36..4aa8900 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 15ab387..34b2099 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/restapi/client_test.go b/restapi/client_test.go index e392b29..1165fe5 100644 --- a/restapi/client_test.go +++ b/restapi/client_test.go @@ -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() diff --git a/restapi/errors_test.go b/restapi/errors_test.go new file mode 100644 index 0000000..3902ce1 --- /dev/null +++ b/restapi/errors_test.go @@ -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, "Test Body!") + } + + 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 +}