Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.

Commit

Permalink
Mock Github calls in tests (#6)
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
tinakurian authored and sbose78 committed Nov 5, 2018
1 parent a595800 commit a0dc0ba
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 34 deletions.
37 changes: 19 additions & 18 deletions Gopkg.lock

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

1 change: 1 addition & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ required = [
"github.com/goadesign/goa/goagen/utils",
"github.com/goadesign/goa/goatest",
"github.com/google/go-github/github",
"github.com/fabric8-services/fabric8-common/token",
"github.com/onsi/ginkgo/ginkgo",
]

Expand Down
2 changes: 1 addition & 1 deletion controllers/build-tool-detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"errors"
"fmt"

"github.com/goadesign/goa"
"github.com/fabric8-services/build-tool-detector/app"
"github.com/fabric8-services/build-tool-detector/config"
errs "github.com/fabric8-services/build-tool-detector/controllers/error"
"github.com/fabric8-services/build-tool-detector/domain/repository"
"github.com/fabric8-services/build-tool-detector/domain/repository/github"
"github.com/fabric8-services/build-tool-detector/domain/types"
"github.com/fabric8-services/build-tool-detector/log"
"github.com/goadesign/goa"
)

var (
Expand Down
84 changes: 75 additions & 9 deletions controllers/build-tool-detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,68 @@ package controllers_test
import (
"io/ioutil"

"github.com/fabric8-services/build-tool-detector/app/test"
"github.com/fabric8-services/build-tool-detector/config"
controllers "github.com/fabric8-services/build-tool-detector/controllers"
"github.com/goadesign/goa"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
"github.com/fabric8-services/build-tool-detector/app/test"
"github.com/fabric8-services/build-tool-detector/config"
controllers "github.com/fabric8-services/build-tool-detector/controllers"
"gopkg.in/h2non/gock.v1"
)

var _ = Describe("BuildToolDetector", func() {
var configuration config.Configuration

BeforeSuite(func() {
viper.SetConfigName("config")
viper.AddConfigPath("../")
viper.ReadInConfig()
viper.Unmarshal(&configuration)
Context("Configuration", func() {
var service *goa.Service
var configuration config.Configuration

BeforeEach(func() {
service = goa.New("build-tool-detector")

viper.SetConfigName("config-template")
viper.AddConfigPath("../")
viper.ReadInConfig()
viper.Unmarshal(&configuration)
configuration.Github.ClientID = ""
configuration.Github.ClientSecret = ""
})
AfterEach(func() {
gock.Off()
})

It("Configuration incorrect - No github_client_id / github_client_secret", func() {
bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/not_found_branch.json")
Expect(err).Should(BeNil())

gock.New("https://api.github.com").
Get("/repos/fabric8-launcher/launcher-backend/branches/master").
Reply(404).
BodyString(string(bodyString))

bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/not_found_repo_branch.json")
Expect(err).Should(BeNil())
gock.New("https://api.github.com").
Get("/repos/fabric8-launcher/launcher-backend/contents/pom.xml").
Reply(404).
BodyString(string(bodyString))
test.ShowBuildToolDetectorNotFound(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, configuration), "https://github.com/fabric8-launcher/launcher-backend/tree/master", nil)
})
})

Context("Internal Server Error", func() {
var service *goa.Service
var configuration config.Configuration

BeforeEach(func() {
service = goa.New("build-tool-detector")

viper.SetConfigName("config-template")
viper.AddConfigPath("../")
viper.ReadInConfig()
viper.Unmarshal(&configuration)
configuration.Github.ClientID = "test"
configuration.Github.ClientSecret = "test"
})
AfterEach(func() {
gock.Off()
Expand Down Expand Up @@ -94,8 +132,17 @@ var _ = Describe("BuildToolDetector", func() {

Context("Okay", func() {
var service *goa.Service
var configuration config.Configuration

BeforeEach(func() {
service = goa.New("build-tool-detector")

viper.SetConfigName("config-template")
viper.AddConfigPath("../")
viper.ReadInConfig()
viper.Unmarshal(&configuration)
configuration.Github.ClientID = "test"
configuration.Github.ClientSecret = "test"
})
AfterEach(func() {
gock.Off()
Expand Down Expand Up @@ -177,5 +224,24 @@ var _ = Describe("BuildToolDetector", func() {
_, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, configuration), "https://github.com/fabric8-launcher/launcher-backend/tree/master", nil)
Expect(buildTool.BuildToolType).Should(Equal("maven"), "buildTool should not be empty")
})

It("Recognize NodeJS - Branch included in URL", func() {
bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_ui/ok_branch.json")
Expect(err).Should(BeNil())

gock.New("https://api.github.com").
Get("/repos/fabric8-ui/fabric8-ui/branches/master").
Reply(200).
BodyString(string(bodyString))

bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_ui/ok_contents.json")
Expect(err).Should(BeNil())
gock.New("https://api.github.com").
Get("/repos/fabric8-ui/fabric8-ui/contents/package.json").
Reply(200).
BodyString(string(bodyString))
_, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, configuration), "https://github.com/fabric8-ui/fabric8-ui/tree/master", nil)
Expect(buildTool.BuildToolType).Should(Equal("nodejs"), "buildTool should be nodejs")
})
})
})
2 changes: 1 addition & 1 deletion controllers/error/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"errors"
"net/http"

. "github.com/fabric8-services/build-tool-detector/controllers/error"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/fabric8-services/build-tool-detector/controllers/error"
)

var _ = Describe("Error", func() {
Expand Down
98 changes: 98 additions & 0 deletions controllers/test/mock/fabric8_ui/ok_branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"name": "master",
"commit": {
"sha": "395c7d63f8a0123487d66f3156429404f170a910",
"node_id": "MDY6Q29tbWl0NzM4MjQ3MzY6Mzk1YzdkNjNmOGEwMTIzNDg3ZDY2ZjMxNTY0Mjk0MDRmMTcwYTkxMA==",
"commit": {
"author": {
"name": "Christian Vogt",
"email": "[email protected]",
"date": "2018-11-05T04:03:12Z"
},
"committer": {
"name": "Joshua Wilson",
"email": "[email protected]",
"date": "2018-11-05T04:03:12Z"
},
"message": "fix(package): up-version the merge package due to vulnerability (#3403)",
"tree": {
"sha": "5e91e8b1119a824a3dac7fc5e49be70125a98ddc",
"url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/git/trees/5e91e8b1119a824a3dac7fc5e49be70125a98ddc"
},
"url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/git/commits/395c7d63f8a0123487d66f3156429404f170a910",
"comment_count": 0,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/commits/395c7d63f8a0123487d66f3156429404f170a910",
"html_url": "https://github.com/fabric8-ui/fabric8-ui/commit/395c7d63f8a0123487d66f3156429404f170a910",
"comments_url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/commits/395c7d63f8a0123487d66f3156429404f170a910/comments",
"author": {
"login": "christianvogt",
"id": 14068621,
"node_id": "MDQ6VXNlcjE0MDY4NjIx",
"avatar_url": "https://avatars3.githubusercontent.com/u/14068621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/christianvogt",
"html_url": "https://github.com/christianvogt",
"followers_url": "https://api.github.com/users/christianvogt/followers",
"following_url": "https://api.github.com/users/christianvogt/following{/other_user}",
"gists_url": "https://api.github.com/users/christianvogt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/christianvogt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/christianvogt/subscriptions",
"organizations_url": "https://api.github.com/users/christianvogt/orgs",
"repos_url": "https://api.github.com/users/christianvogt/repos",
"events_url": "https://api.github.com/users/christianvogt/events{/privacy}",
"received_events_url": "https://api.github.com/users/christianvogt/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "joshuawilson",
"id": 1184371,
"node_id": "MDQ6VXNlcjExODQzNzE=",
"avatar_url": "https://avatars2.githubusercontent.com/u/1184371?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/joshuawilson",
"html_url": "https://github.com/joshuawilson",
"followers_url": "https://api.github.com/users/joshuawilson/followers",
"following_url": "https://api.github.com/users/joshuawilson/following{/other_user}",
"gists_url": "https://api.github.com/users/joshuawilson/gists{/gist_id}",
"starred_url": "https://api.github.com/users/joshuawilson/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/joshuawilson/subscriptions",
"organizations_url": "https://api.github.com/users/joshuawilson/orgs",
"repos_url": "https://api.github.com/users/joshuawilson/repos",
"events_url": "https://api.github.com/users/joshuawilson/events{/privacy}",
"received_events_url": "https://api.github.com/users/joshuawilson/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "83d9783dea4d80d3bc68205b70f5359b745dc694",
"url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/commits/83d9783dea4d80d3bc68205b70f5359b745dc694",
"html_url": "https://github.com/fabric8-ui/fabric8-ui/commit/83d9783dea4d80d3bc68205b70f5359b745dc694"
}
]
},
"links": {
"self": "https://api.github.com/repos/fabric8-ui/fabric8-ui/branches/master",
"html": "https://github.com/fabric8-ui/fabric8-ui/tree/master"
},
"protected": true,
"protection": {
"enabled": true,
"required_status_checks": {
"enforcement_level": "non_admins",
"contexts": [
"ci.centos.org PR build",
"alien-ike/test-keeper"
]
}
},
"protection_url": "https://api.github.com/repos/fabric8-ui/fabric8-ui/branches/master/protection"
}
18 changes: 18 additions & 0 deletions controllers/test/mock/fabric8_ui/ok_contents.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion domain/repository/github/github_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"errors"
"net/http"

"github.com/google/go-github/github"
"github.com/fabric8-services/build-tool-detector/config"
"github.com/fabric8-services/build-tool-detector/domain/types"
"github.com/google/go-github/github"
)

const (
Expand Down
Loading

0 comments on commit a0dc0ba

Please sign in to comment.