Add test for GitHub remote Authorize()

To test that it correctly authorises a valid user, prior to me extending it.
This only tests the happy path because simulating oAuth failures is going
to be quite hard at this stage.
This commit is contained in:
Dan Carley 2015-01-15 11:53:22 +00:00
parent a608f5ef82
commit 789adf90e4
2 changed files with 85 additions and 0 deletions

View file

@ -1,6 +1,9 @@
package github package github
import ( import (
"fmt"
"net/http"
"net/http/httptest"
"testing" "testing"
"github.com/drone/drone/plugin/remote/github/testdata" "github.com/drone/drone/plugin/remote/github/testdata"
@ -90,5 +93,20 @@ func Test_Github(t *testing.T) {
g.It("Should parse a commit hook") g.It("Should parse a commit hook")
g.It("Should parse a pull request hook") g.It("Should parse a pull request hook")
g.It("Should authorize a valid user", func() {
var resp = httptest.NewRecorder()
var state = "validstate"
var req, _ = http.NewRequest(
"GET",
fmt.Sprintf("%s/?code=sekret&state=%s", server.URL, state),
nil,
)
req.AddCookie(&http.Cookie{Name: "github_state", Value: state})
var login, err = github.Authorize(resp, req)
g.Assert(err == nil).IsTrue()
g.Assert(login == nil).IsFalse()
})
}) })
} }

View file

@ -15,6 +15,15 @@ func NewServer() *httptest.Server {
// evaluate the path to serve a dummy data file // evaluate the path to serve a dummy data file
switch r.URL.Path { switch r.URL.Path {
case "/login/oauth/access_token":
w.Write(accessTokenPayload)
return
case "/user":
w.Write(userPayload)
return
case "/user/emails":
w.Write(userEmailsPayload)
return
case "/user/repos": case "/user/repos":
w.Write(userReposPayload) w.Write(userReposPayload)
return return
@ -56,6 +65,64 @@ func NewServer() *httptest.Server {
return server return server
} }
var accessTokenPayload = []byte(`access_token=sekret&scope=repo%2Cuser%3Aemail&token_type=bearer`)
var userPayload = []byte(`
{
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"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,
"name": "monalisa octocat",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"hireable": false,
"bio": "There once was...",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z",
"total_private_repos": 100,
"owned_private_repos": 100,
"private_gists": 81,
"disk_usage": 10000,
"collaborators": 8,
"plan": {
"name": "Medium",
"space": 400,
"private_repos": 20,
"collaborators": 0
}
}
`)
var userEmailsPayload = []byte(`
[
{
"email": "octocat@github.com",
"verified": true,
"primary": true
}
]
`)
// sample repository list // sample repository list
var userReposPayload = []byte(` var userReposPayload = []byte(`
[ [