Skip to content

Commit de5eb36

Browse files
authored
Add open api tests (#122)
* Adds README under /openApiTests Explaining intention of the folder * Adds Open API test based on Prism And instructions on how to run it locally * Adds Github Actions workflow To run Open API test as part of a CI process * Run CI process every 5 minutes As part of a "soak" test to confirm that the CI process is reliable * Revert "Run CI process every 5 minutes" This reverts commit 6babbb9. * Remove CI using Github Actions * Removes CI badge from README * Merge OpenAPI tests with existing tests * Use testify assert Makes test easier to read * Removes code no longer required * Fixes typo
1 parent de25448 commit de5eb36

File tree

7 files changed

+71
-17
lines changed

7 files changed

+71
-17
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ endif
1717
test: env ## run go test all
1818
go test -v ./tests
1919

20+
test/swagger:
21+
env BITBUCKET_API_BASE_URL=http://0.0.0.0:4010 go test -v ./...
22+
2023
help: ## print this help
2124
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
2225

23-
.PHONY: test help
26+
.PHONY: test test/swagger help

client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ import (
2323

2424
const DEFAULT_PAGE_LENGTH = 10
2525
const DEFAULT_MAX_DEPTH = 1
26+
const DEFAULT_BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0"
27+
28+
func apiBaseUrl() string {
29+
ev := os.Getenv("BITBUCKET_API_BASE_URL")
30+
if ev != "" {
31+
return ev
32+
}
33+
34+
return DEFAULT_BITBUCKET_API_BASE_URL
35+
}
2636

2737
type Client struct {
2838
Auth *auth
@@ -125,7 +135,7 @@ func NewBasicAuth(u, p string) *Client {
125135
}
126136

127137
func injectClient(a *auth) *Client {
128-
c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH, apiBaseURL: "https://api.bitbucket.org/2.0"}
138+
c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH, apiBaseURL: apiBaseUrl()}
129139
c.Repositories = &Repositories{
130140
c: c,
131141
PullRequests: &PullRequests{c: c},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/mattn/go-colorable v0.0.9 // indirect
1010
github.com/mattn/go-isatty v0.0.3 // indirect
1111
github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238
12+
github.com/stretchr/testify v1.6.1
1213
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01
1314
golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff
1415
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ=
24
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
35
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
@@ -10,6 +12,11 @@ github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI
1012
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
1113
github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238 h1:+MZW2uvHgN8kYvksEN3f7eFL2wpzk0GxmlFsMybWc7E=
1214
github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
15+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
19+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1320
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 h1:po1f06KS05FvIQQA2pMuOWZAUXiy1KYdIf0ElUU2Hhc=
1421
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
1522
golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff h1:WbI0V2v8RZPTlOMrecBqqba3029Z6l7jeFySSGaN/jk=
@@ -20,3 +27,6 @@ golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54 h1:4qAtdeqGYyXU2CfUvLomEFw0c
2027
golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2128
google.golang.org/appengine v1.0.0 h1:dN4LljjBKVChsv0XCSI+zbyzdqrkEwX5LQFUMRSGqOc=
2229
google.golang.org/appengine v1.0.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
30+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
32+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tests/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Tests
2+
3+
As per @ktrysmt's comment [here](https://github.com/ktrysmt/go-bitbucket/pull/122#issuecomment-758373984) tests are being changed so they can be run against both:
4+
1. The actual Bitbucket API
5+
1. The Swagger documentation from Bitbucket ([https://bitbucket.org/api/swagger.json](https://bitbucket.org/api/swagger.json)), using [Stoplight's Prism](https://stoplight.io/open-source/prism)
6+
7+
The latter will eventually be run as part of a CI process using [Github Actions](https://github.com/features/actions).
8+
9+
## Running tests locally against the Bitbucket API
10+
11+
Please refer to [../README.md](../README.md).
12+
13+
# Running test locally against Prism
14+
15+
Run in a shell terminal:
16+
```
17+
docker run --rm -it -p 4010:4010 stoplight/prism:3 mock -h 0.0.0.0 https://bitbucket.org/api/swagger.json
18+
```
19+
20+
Then in another shell terminal session run:
21+
```
22+
make test/swagger
23+
```

tests/user_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"github.com/stretchr/testify/assert"
45
"os"
56
"testing"
67

@@ -9,25 +10,31 @@ import (
910

1011
func TestProfile(t *testing.T) {
1112

12-
user := os.Getenv("BITBUCKET_TEST_USERNAME")
13-
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
14-
15-
if user == "" {
16-
t.Error("BITBUCKET_TEST_USERNAME is empty.")
17-
}
18-
19-
if pass == "" {
20-
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
21-
}
13+
user := getUsername()
14+
pass := getPassword()
2215

2316
c := bitbucket.NewBasicAuth(user, pass)
2417

2518
res, err := c.User.Profile()
26-
if err != nil {
27-
t.Error(err)
19+
20+
assert.NoError(t, err)
21+
assert.NotNil(t, res)
22+
}
23+
24+
func getUsername() string {
25+
ev := os.Getenv("BITBUCKET_TEST_USERNAME")
26+
if ev != "" {
27+
return ev
2828
}
2929

30-
if res.Username != user {
31-
t.Error("Cannot catch the Profile.username.")
30+
return "example-username"
31+
}
32+
33+
func getPassword() string {
34+
ev := os.Getenv("BITBUCKET_TEST_PASSWORD")
35+
if ev != "" {
36+
return ev
3237
}
38+
39+
return "password"
3340
}

user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type User struct {
2121

2222
// Profile is getting the user data
2323
func (u *User) Profile() (*User, error) {
24-
urlStr := u.c.GetApiBaseURL() + "/user/"
24+
urlStr := u.c.GetApiBaseURL() + "/user"
2525
response, err := u.c.execute("GET", urlStr, "")
2626
if err != nil {
2727
return nil, err

0 commit comments

Comments
 (0)