Created
March 10, 2014 07:01
-
-
Save anonymous/9460601 to your computer and use it in GitHub Desktop.
http test fixture example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mux is the HTTP request multiplexer used with the test server. | |
var mux *http.ServeMux | |
// server is a test HTTP server used to provide mock API responses. | |
var server *httptest.Server | |
func SetupFixtures() { | |
dbtest.Setup() | |
// test server | |
mux = http.NewServeMux() | |
server = httptest.NewServer(mux) | |
url, _ := url.Parse(server.URL) | |
// set database to use a localhost url for GitHub | |
settings := model.Settings{} | |
settings.GitHubKey = "123" | |
settings.GitHubSecret = "abc" | |
settings.GitHubApiUrl = url.String() // normall would be "https://api.github.com" | |
settings.GitHubDomain = url.Host // normally would be "github.com" | |
settings.Scheme = url.Scheme | |
settings.Domain = "localhost" | |
database.SaveSettings(&settings) | |
// github repository not found | |
mux.HandleFunc("/repos/octocat/notfound", func(w http.ResponseWriter, r *http.Request) { | |
http.NotFound(w, r) | |
}) | |
// user does not have permissions to create the hook | |
mux.HandleFunc("/repos/octocat/readonly/hooks", func(w http.ResponseWriter, r *http.Request) { | |
http.Error(w, "Forbidden", http.StatusForbidden) | |
}) | |
// user does not have permissions to create the hook | |
mux.HandleFunc("/repos/octocat/readonly/writable", func(w http.ResponseWriter, r *http.Request) { | |
http.Error(w, "Forbidden", http.StatusForbidden) | |
}) | |
// github repository found, but forbidden to create hooks | |
mux.HandleFunc("/repos/octocat/readonly", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, `{"id":1296269,"name":"readonly","full_name":"octocat/readonly","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/readonly","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/readonly","forks_url":"https://api.github.com/repos/octocat/readonly/forks","keys_url":"https://api.github.com/repos/octocat/readonly/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/readonly/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/readonly/teams","hooks_url":"https://api.github.com/repos/octocat/readonly/hooks","issue_events_url":"https://api.github.com/repos/octocat/readonly/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/readonly/events","assignees_url":"https://api.github.com/repos/octocat/readonly/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/readonly/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/readonly/tags","blobs_url":"https://api.github.com/repos/octocat/readonly/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/readonly/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/readonly/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/readonly/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/readonly/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/readonly/languages","stargazers_url":"https://api.github.com/repos/octocat/readonly/stargazers","contributors_url":"https://api.github.com/repos/octocat/readonly/contributors","subscribers_url":"https://api.github.com/repos/octocat/readonly/subscribers","subscription_url":"https://api.github.com/repos/octocat/readonly/subscription","commits_url":"https://api.github.com/repos/octocat/readonly/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/readonly/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/readonly/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/readonly/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/readonly/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/readonly/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/readonly/merges","archive_url":"https://api.github.com/repos/octocat/readonly/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/readonly/downloads","issues_url":"https://api.github.com/repos/octocat/readonly/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/readonly/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/readonly/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/readonly/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/readonly/labels{/name}","releases_url":"https://api.github.com/repos/octocat/readonly/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/readonly.git","ssh_url":"git@github.com:octocat/readonly.git","clone_url":"https://github.com/octocat/readonly.git","svn_url":"https://github.com/octocat/readonly","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) | |
}) | |
// github repository that is writable | |
mux.HandleFunc("/repos/octocat/writable", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, `{"id":1296269,"name":"writable","full_name":"octocat/writable","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/writable","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/writable","forks_url":"https://api.github.com/repos/octocat/writable/forks","keys_url":"https://api.github.com/repos/octocat/writable/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/writable/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/writable/teams","hooks_url":"https://api.github.com/repos/octocat/writable/hooks","issue_events_url":"https://api.github.com/repos/octocat/writable/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/writable/events","assignees_url":"https://api.github.com/repos/octocat/writable/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/writable/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/writable/tags","blobs_url":"https://api.github.com/repos/octocat/writable/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/writable/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/writable/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/writable/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/writable/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/writable/languages","stargazers_url":"https://api.github.com/repos/octocat/writable/stargazers","contributors_url":"https://api.github.com/repos/octocat/writable/contributors","subscribers_url":"https://api.github.com/repos/octocat/writable/subscribers","subscription_url":"https://api.github.com/repos/octocat/writable/subscription","commits_url":"https://api.github.com/repos/octocat/writable/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/writable/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/writable/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/writable/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/writable/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/writable/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/writable/merges","archive_url":"https://api.github.com/repos/octocat/writable/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/writable/downloads","issues_url":"https://api.github.com/repos/octocat/writable/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/writable/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/writable/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/writable/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/writable/labels{/name}","releases_url":"https://api.github.com/repos/octocat/writable/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/writable.git","ssh_url":"git@github.com:octocat/writable.git","clone_url":"https://github.com/octocat/writable.git","svn_url":"https://github.com/octocat/writable","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) | |
}) | |
} | |
func TeardownFixtures() { | |
dbtest.Teardown() | |
server.Close() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tests the ability to create GitHub repositories. | |
func Test_GitHubCreate(t *testing.T) { | |
// seed the database with values | |
SetupFixtures() | |
defer TeardownFixtures() | |
// mock request | |
req := http.Request{} | |
req.Form = url.Values{} | |
// get user that will add repositories | |
user, _ := database.GetUser(1) | |
Convey("GitHub Setup", t, func() { | |
SkipConvey("Successfully Setup", func() { | |
}) | |
SkipConvey("Successfully Setup Private", func() { | |
}) | |
SkipConvey("Successfully Setup Team", func() { | |
}) | |
Convey("Invalid GitHub Repository", func() { | |
req.Form.Set("owner", "octocat") | |
req.Form.Set("name", "notfound") | |
req.Form.Set("team", "") | |
res := httptest.NewRecorder() | |
err := handler.RepoCreateGithub(res, &req, user) | |
Convey("Not Found", func() { | |
So(err, ShouldNotBeNil) | |
So(err.Error(), ShouldEqual, "Unable to find GitHub repository octocat/notfound.") | |
}) | |
}) | |
Convey("Failure to setup Commit Hook", func() { | |
req.Form.Set("owner", "octocat") | |
req.Form.Set("name", "readonly") | |
req.Form.Set("team", "") | |
res := httptest.NewRecorder() | |
err := handler.RepoCreateGithub(res, &req, user) | |
Convey("Forbidden", func() { | |
So(err, ShouldNotBeNil) | |
So(err.Error(), ShouldEqual, "Unable to add Hook to your GitHub repository.") | |
}) | |
}) | |
SkipConvey("Failure to setup SSH Key", func() { | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment