Example #1
0
func TestUsersService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")

		rr := regexp.MustCompile(`users\?since=\d+`)
		assert.Tf(t, rr.MatchString(r.URL.String()), "Regexp should match users?since=\\d+")

		header := w.Header()
		link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="first"`, testURLOf("users?since=135"), testURLOf("users{?since}"))
		header.Set("Link", link)
		respondWithJSON(w, loadFixture("users.json"))
	})

	url, err := UserURL.Expand(M{"since": 1})
	assert.Equal(t, nil, err)

	q := url.Query()
	q.Set("since", "1")
	url.RawQuery = q.Encode()
	allUsers, result := client.Users(url).All()

	assert.T(t, !result.HasError())
	assert.Equal(t, 1, len(allUsers))
	assert.Equal(t, testURLStringOf("users?since=135"), string(*result.NextPage))

	nextPageURL, err := result.NextPage.Expand(nil)
	assert.Equal(t, nil, err)

	allUsers, result = client.Users(nextPageURL).All()
	assert.T(t, !result.HasError())
	assert.Equal(t, 1, len(allUsers))
}
Example #2
0
func TestAuthorizationsService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("authorizations.json"))
	})

	url, err := AuthorizationsURL.Expand(nil)
	assert.Equal(t, nil, err)

	auths, result := client.Authorizations(url).All()
	assert.T(t, !result.HasError())

	firstAuth := auths[0]
	assert.Equal(t, 1, firstAuth.ID)
	assert.Equal(t, "https://api.github.com/authorizations/1", firstAuth.URL)
	assert.Equal(t, "456", firstAuth.Token)
	assert.Equal(t, "", firstAuth.Note)
	assert.Equal(t, "", firstAuth.NoteURL)
	assert.Equal(t, "2012-11-16 01:05:51 +0000 UTC", firstAuth.CreatedAt.String())
	assert.Equal(t, "2013-08-21 03:29:51 +0000 UTC", firstAuth.UpdatedAt.String())

	app := App{ClientID: "123", URL: "http://localhost:8080", Name: "Test"}
	assert.Equal(t, app, firstAuth.App)

	assert.Equal(t, 2, len(firstAuth.Scopes))
	scopes := []string{"repo", "user"}
	assert.T(t, reflect.DeepEqual(firstAuth.Scopes, scopes))
}
Example #3
0
func TestNewArgs(t *testing.T) {
	args := NewArgs([]string{})
	assert.Equal(t, "", args.Command)
	assert.Equal(t, 0, args.ParamsSize())

	args = NewArgs([]string{"command"})
	assert.Equal(t, "command", args.Command)
	assert.Equal(t, 0, args.ParamsSize())

	args = NewArgs([]string{"command", "args"})
	assert.Equal(t, "command", args.Command)
	assert.Equal(t, 1, args.ParamsSize())

	args = NewArgs([]string{"--noop", "command", "args"})
	assert.Equal(t, "command", args.Command)
	assert.Equal(t, 1, args.ParamsSize())
	assert.T(t, args.Noop)

	args = NewArgs([]string{"--version"})
	assert.Equal(t, "version", args.Command)
	assert.Equal(t, 0, args.ParamsSize())

	args = NewArgs([]string{"--help"})
	assert.Equal(t, "help", args.Command)
	assert.Equal(t, 0, args.ParamsSize())

	args = NewArgs([]string{"--noop", "--version"})
	assert.T(t, args.Noop)
	assert.Equal(t, "version", args.Command)

	args = NewArgs([]string{"-c", "foo=bar", "--git-dir=path", "--bare"})
	assert.Equal(t, 5, len(args.GlobalFlags))
	assert.Equal(t, "-c foo=bar --bare --git-dir path", strings.Join(args.GlobalFlags, " "))
}
Example #4
0
func TestAuthorizationsService_Create(t *testing.T) {
	setup()
	defer tearDown()

	params := AuthorizationParams{Scopes: []string{"public_repo"}}

	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
		var authParams AuthorizationParams
		json.NewDecoder(r.Body).Decode(&authParams)
		assert.T(t, reflect.DeepEqual(authParams, params))

		testMethod(t, r, "POST")
		respondWithJSON(w, loadFixture("create_authorization.json"))
	})

	url, err := AuthorizationsURL.Expand(nil)
	assert.Equal(t, nil, err)

	auth, _ := client.Authorizations(url).Create(params)

	assert.Equal(t, 3844190, auth.ID)
	assert.Equal(t, "https://api.github.com/authorizations/3844190", auth.URL)
	assert.Equal(t, "123", auth.Token)
	assert.Equal(t, "", auth.Note)
	assert.Equal(t, "", auth.NoteURL)
	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.CreatedAt.String())
	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.UpdatedAt.String())

	app := App{ClientID: "00000000000000000000", URL: "http://developer.github.com/v3/oauth/#oauth-authorizations-api", Name: "GitHub API"}
	assert.Equal(t, app, auth.App)

	assert.Equal(t, 1, len(auth.Scopes))
	scopes := []string{"public_repo"}
	assert.T(t, reflect.DeepEqual(auth.Scopes, scopes))
}
Example #5
0
func TestRepositoresService_CreateFork(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/jingweno/octokat/forks", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		testBody(t, r, "{\"organization\":\"github\"}\n")
		respondWithJSON(w, loadFixture("create_repository.json"))
	})

	url, err := ForksURL.Expand(M{"owner": "jingweno", "repo": "octokat"})
	assert.Equal(t, nil, err)

	repo, result := client.Repositories(url).Create(M{"organization": "github"})

	assert.T(t, !result.HasError())
	assert.Equal(t, 1296269, repo.ID)
	assert.Equal(t, "Hello-World", repo.Name)
	assert.Equal(t, "octocat/Hello-World", repo.FullName)
	assert.Equal(t, "This is your first repo", repo.Description)
	assert.T(t, !repo.Private)
	assert.T(t, repo.Fork)
	assert.Equal(t, "https://api.github.com/repos/octocat/Hello-World", repo.URL)
	assert.Equal(t, "https://github.com/octocat/Hello-World", repo.HTMLURL)
	assert.Equal(t, "https://github.com/octocat/Hello-World.git", repo.CloneURL)
	assert.Equal(t, "git://github.com/octocat/Hello-World.git", repo.GitURL)
	assert.Equal(t, "[email protected]:octocat/Hello-World.git", repo.SSHURL)
	assert.Equal(t, "master", repo.MasterBranch)
}
Example #6
0
func TestNewResult_Pageable(t *testing.T) {
	resp := &Response{MediaHeader: &mediaheader.MediaHeader{Relations: hypermedia.Relations{"next": hypermedia.Hyperlink("/path")}}}
	result := newResult(resp, nil)

	assert.Equal(t, "/path", string(*result.NextPage))
	assert.T(t, result.PrevPage == nil)
	assert.T(t, result.LastPage == nil)
	assert.T(t, result.FirstPage == nil)
}
Example #7
0
func TestTransformRemoteArgs(t *testing.T) {
	os.Setenv("HUB_PROTOCOL", "git")
	github.CreateTestConfigs("jingweno", "123")

	args := NewArgs([]string{"remote", "add", "jingweno"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "jingweno", args.GetParam(1))
	reg := regexp.MustCompile("^git@github\\.com:jingweno/.+\\.git$")
	assert.T(t, reg.MatchString(args.GetParam(2)))

	args = NewArgs([]string{"remote", "add", "mislav"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "mislav", args.GetParam(1))
	reg = regexp.MustCompile("^git://github\\.com/mislav/.+\\.git$")
	assert.T(t, reg.MatchString(args.GetParam(2)))

	args = NewArgs([]string{"remote", "add", "-p", "mislav"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "mislav", args.GetParam(1))
	reg = regexp.MustCompile("^git@github\\.com:mislav/.+\\.git$")
	assert.T(t, reg.MatchString(args.GetParam(2)))

	args = NewArgs([]string{"remote", "add", "origin"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "origin", args.GetParam(1))
	reg = regexp.MustCompile("^git@github\\.com:jingweno/.+\\.git$")
	assert.T(t, reg.MatchString(args.GetParam(2)))

	args = NewArgs([]string{"remote", "add", "jingweno", "[email protected]:jingweno/gh.git"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "jingweno", args.GetParam(1))
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "[email protected]:jingweno/gh.git", args.GetParam(2))

	args = NewArgs([]string{"remote", "add", "-p", "origin", "org/foo"})
	transformRemoteArgs(args)

	assert.Equal(t, 3, args.ParamsSize())
	assert.Equal(t, "origin", args.GetParam(1))
	assert.Equal(t, "add", args.FirstParam())
	assert.Equal(t, "[email protected]:org/foo.git", args.GetParam(2))
}
Example #8
0
func TestAuthTokenNote(t *testing.T) {
	note, err := authTokenNote(1)
	assert.Equal(t, nil, err)

	reg := regexp.MustCompile("hub for (.+)@(.+)")
	assert.T(t, reg.MatchString(note))

	note, err = authTokenNote(2)
	assert.Equal(t, nil, err)

	reg = regexp.MustCompile("hub for (.+)@(.+) 2")
	assert.T(t, reg.MatchString(note))

}
Example #9
0
func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) {
	tempFile, _ := ioutil.TempFile("", "editor-test")
	tempFile.Close()

	ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644)
	editor := Editor{
		Program: "memory",
		File:    tempFile.Name(),
		Topic:   "test",
		openEditor: func(program string, file string) error {
			assert.Equal(t, "memory", program)
			assert.Equal(t, tempFile.Name(), file)
			return fmt.Errorf("error")
		},
	}

	_, err := os.Stat(tempFile.Name())
	assert.Equal(t, nil, err)

	_, err = editor.openAndEdit()
	assert.Equal(t, "error using text editor for test message", fmt.Sprintf("%s", err))

	// file is removed if there's error
	_, err = os.Stat(tempFile.Name())
	assert.T(t, os.IsNotExist(err))
}
Example #10
0
func TestGitDir(t *testing.T) {
	repo := fixtures.SetupTestRepo()
	defer repo.TearDown()

	gitDir, _ := Dir()
	assert.T(t, strings.Contains(gitDir, ".git"))
}
Example #11
0
func TestUploadsService_UploadAsset(t *testing.T) {
	setup()
	defer tearDown()

	file, err := ioutil.TempFile("", "octokit-test-upload-")
	assert.Equal(t, nil, err)
	file.WriteString("this is a test")

	fi, err := file.Stat()
	assert.Equal(t, nil, err)
	file.Close()

	mux.HandleFunc("/repos/octokit/Hello-World/releases/123/assets", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		testHeader(t, r, "Content-Type", "text/plain")
		assert.Equal(t, fi.Size(), r.ContentLength)
		respondWithStatus(w, 201)
	})

	link := Hyperlink("/repos/octokit/Hello-World/releases/123/assets{?name}")
	url, err := link.Expand(M{"name": fi.Name()})
	assert.Equal(t, nil, err)

	open, _ := os.Open(file.Name())
	result := client.Uploads(url).UploadAsset(open, "text/plain", fi.Size())
	fmt.Println(result)
	assert.T(t, !result.HasError())

	assert.Equal(t, 201, result.Response.StatusCode)
}
Example #12
0
func TestGitTreesService_One_Recursive(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/pengwynn/flint/git/trees/master", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("tree_recursive.json"))
	})

	url, err := GitTreesURL.Expand(M{
		"owner":     "pengwynn",
		"repo":      "flint",
		"sha":       "master",
		"recursive": "1",
	})
	assert.Equal(t, nil, err)
	tree, result := client.GitTrees(url).One()

	assert.T(t, !result.HasError())
	assert.Equal(t, "9c1337e761bbd517f3cc1b5acb9373b17f4810e8", tree.Sha)
	assert.Equal(t, "https://api.github.com/repos/pengwynn/flint/git/trees/9c1337e761bbd517f3cc1b5acb9373b17f4810e8", tree.URL)

	entries := tree.Tree
	assert.Equal(t, 15, len(entries))
}
Example #13
0
func TestDirIsNotEmpty(t *testing.T) {
	dir := createTempDir(t)
	defer os.RemoveAll(dir)
	ioutil.TempFile(dir, "gh-utils-test-")

	assert.T(t, !isEmptyDir(dir))
}
Example #14
0
func TestRepositoresService_Create(t *testing.T) {
	setup()
	defer tearDown()

	params := Repository{}
	params.Name = "Hello-World"
	params.Description = "This is your first repo"
	params.Homepage = "https://github.com"
	params.Private = false
	params.HasIssues = true
	params.HasWiki = true
	params.HasDownloads = true

	mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
		var repoParams Repository
		json.NewDecoder(r.Body).Decode(&repoParams)
		assert.Equal(t, params.Name, repoParams.Name)
		assert.Equal(t, params.Description, repoParams.Description)
		assert.Equal(t, params.Homepage, repoParams.Homepage)
		assert.Equal(t, params.Private, repoParams.Private)
		assert.Equal(t, params.HasIssues, repoParams.HasIssues)
		assert.Equal(t, params.HasWiki, repoParams.HasWiki)
		assert.Equal(t, params.HasDownloads, repoParams.HasDownloads)

		testMethod(t, r, "POST")
		respondWithJSON(w, loadFixture("create_repository.json"))
	})

	url, err := UserRepositoriesURL.Expand(nil)
	assert.Equal(t, nil, err)

	repo, result := client.Repositories(url).Create(params)

	assert.T(t, !result.HasError())
	assert.Equal(t, 1296269, repo.ID)
	assert.Equal(t, "Hello-World", repo.Name)
	assert.Equal(t, "octocat/Hello-World", repo.FullName)
	assert.Equal(t, "This is your first repo", repo.Description)
	assert.T(t, !repo.Private)
	assert.T(t, repo.Fork)
	assert.Equal(t, "https://api.github.com/repos/octocat/Hello-World", repo.URL)
	assert.Equal(t, "https://github.com/octocat/Hello-World", repo.HTMLURL)
	assert.Equal(t, "https://github.com/octocat/Hello-World.git", repo.CloneURL)
	assert.Equal(t, "git://github.com/octocat/Hello-World.git", repo.GitURL)
	assert.Equal(t, "[email protected]:octocat/Hello-World.git", repo.SSHURL)
	assert.Equal(t, "master", repo.MasterBranch)
}
Example #15
0
func TestPullRequestService_Post(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octokit/go-octokit/pulls", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		testBody(t, r,
			"{\"base\":\"base\",\"head\":\"head\",\"title\":\"title\",\"body\":\"body\"}\n")
		respondWithJSON(w, loadFixture("pull_request.json"))
	})

	url, err := PullRequestsURL.Expand(M{"owner": "octokit", "repo": "go-octokit"})
	assert.Equal(t, nil, err)

	params := PullRequestParams{
		Base:  "base",
		Head:  "head",
		Title: "title",
		Body:  "body",
	}
	pr, result := client.PullRequests(url).Create(params)

	assert.T(t, !result.HasError())
	assert.Equal(t, 1, pr.ChangedFiles)
	assert.Equal(t, 1, pr.Deletions)
	assert.Equal(t, 1, pr.Additions)
	assert.Equal(t, "aafce5dfc44270f35270b24677abbb859b20addf", pr.MergeCommitSha)
	assert.Equal(t, "2013-06-09 00:53:38 +0000 UTC", pr.MergedAt.String())
	assert.Equal(t, "2013-06-09 00:53:38 +0000 UTC", pr.ClosedAt.String())
	assert.Equal(t, "2013-06-19 00:35:24 +0000 UTC", pr.UpdatedAt.String())
	assert.Equal(t, "2013-06-09 00:52:12 +0000 UTC", pr.CreatedAt.String())
	assert.Equal(t, "typo", pr.Body)
	assert.Equal(t, "Update README.md", pr.Title)
	assert.Equal(t, "https://api.github.com/repos/jingweno/octokat/pulls/1", pr.URL)
	assert.Equal(t, 6206442, pr.ID)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1", pr.HTMLURL)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1.diff", pr.DiffURL)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1.patch", pr.PatchURL)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1", pr.IssueURL)
	assert.Equal(t, 1, pr.Number)
	assert.Equal(t, "closed", pr.State)
	assert.T(t, nil == pr.Assignee)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1/commits", pr.CommitsURL)
	assert.Equal(t, "https://github.com/jingweno/octokat/pull/1/comments", pr.ReviewCommentsURL)
	assert.Equal(t, "/repos/jingweno/octokat/pulls/comments/{number}", pr.ReviewCommentURL)
	assert.Equal(t, "https://api.github.com/repos/jingweno/octokat/issues/1/comments", pr.CommentsURL)
}
Example #16
0
func TestReleasesService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/jingweno/gh/releases", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("releases.json"))
	})

	url, err := ReleasesURL.Expand(M{"owner": "jingweno", "repo": "gh"})
	assert.Equal(t, nil, err)

	releases, result := client.Releases(url).All()
	assert.T(t, !result.HasError())
	assert.Equal(t, 1, len(releases))

	firstRelease := releases[0]
	assert.Equal(t, 50013, firstRelease.ID)
	assert.Equal(t, "v0.23.0", firstRelease.TagName)
	assert.Equal(t, "master", firstRelease.TargetCommitish)
	assert.Equal(t, "v0.23.0", firstRelease.Name)
	assert.T(t, !firstRelease.Draft)
	assert.T(t, !firstRelease.Prerelease)
	assert.Equal(t, "* Windows works!: https://github.com/jingweno/gh/commit/6cb80cb09fd9f624a64d85438157955751a9ac70", firstRelease.Body)
	assert.Equal(t, "https://api.github.com/repos/jingweno/gh/releases/50013", firstRelease.URL)
	assert.Equal(t, "https://api.github.com/repos/jingweno/gh/releases/50013/assets", firstRelease.AssetsURL)
	assert.Equal(t, "https://uploads.github.com/repos/jingweno/gh/releases/50013/assets{?name}", string(firstRelease.UploadURL))
	assert.Equal(t, "https://github.com/jingweno/gh/releases/v0.23.0", firstRelease.HTMLURL)
	assert.Equal(t, "2013-09-23 00:59:10 +0000 UTC", firstRelease.CreatedAt.String())
	assert.Equal(t, "2013-09-23 01:07:56 +0000 UTC", firstRelease.PublishedAt.String())

	firstReleaseAssets := firstRelease.Assets
	assert.Equal(t, 8, len(firstReleaseAssets))

	firstAsset := firstReleaseAssets[0]
	assert.Equal(t, 20428, firstAsset.ID)
	assert.Equal(t, "gh_0.23.0-snapshot_amd64.deb", firstAsset.Name)
	assert.Equal(t, "gh_0.23.0-snapshot_amd64.deb", firstAsset.Label)
	assert.Equal(t, "application/x-deb", firstAsset.ContentType)
	assert.Equal(t, "uploaded", firstAsset.State)
	assert.Equal(t, 1562984, firstAsset.Size)
	assert.Equal(t, 0, firstAsset.DownloadCount)
	assert.Equal(t, "https://api.github.com/repos/jingweno/gh/releases/assets/20428", firstAsset.URL)
	assert.Equal(t, "2013-09-23 01:05:20 +0000 UTC", firstAsset.CreatedAt.String())
	assert.Equal(t, "2013-09-23 01:07:56 +0000 UTC", firstAsset.UpdatedAt.String())
}
Example #17
0
func TestRepositoresService_One(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/jingweno/octokat", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("repository.json"))
	})

	url, err := RepositoryURL.Expand(M{"owner": "jingweno", "repo": "octokat"})
	assert.Equal(t, nil, err)

	repo, result := client.Repositories(url).One()

	assert.T(t, !result.HasError())
	assert.Equal(t, 10575811, repo.ID)
	assert.Equal(t, "octokat", repo.Name)
	assert.Equal(t, "jingweno/octokat", repo.FullName)
	assert.T(t, !repo.Private)
	assert.T(t, !repo.Fork)
	assert.Equal(t, "https://api.github.com/repos/jingweno/octokat", repo.URL)
	assert.Equal(t, "https://github.com/jingweno/octokat", repo.HTMLURL)
	assert.Equal(t, "https://github.com/jingweno/octokat.git", repo.CloneURL)
	assert.Equal(t, "git://github.com/jingweno/octokat.git", repo.GitURL)
	assert.Equal(t, "[email protected]:jingweno/octokat.git", repo.SSHURL)
	assert.Equal(t, "master", repo.MasterBranch)
	assert.T(t, !repo.Permissions.Admin)
	assert.T(t, !repo.Permissions.Push)
	assert.T(t, repo.Permissions.Pull)
}
Example #18
0
func TestCommitsService_Patch(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octokit/go-octokit/commits/b6d21008bf7553a29ad77ee0a8bb3b66e6f11aa2", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		testHeader(t, r, "Accept", patchMediaType)
		respondWithJSON(w, loadFixture("commit.patch"))
	})

	url, err := CommitsURL.Expand(M{
		"owner": "octokit",
		"repo":  "go-octokit",
		"sha":   "b6d21008bf7553a29ad77ee0a8bb3b66e6f11aa2",
	})
	assert.Equal(t, nil, err)
	patch, result := client.Commits(url).Patch()

	assert.T(t, !result.HasError())
	content, err := ioutil.ReadAll(patch)
	assert.Equal(t, nil, err)
	assert.T(t, len(content) > 0)
}
Example #19
0
func TestUpdater_timeToUpdate(t *testing.T) {
	// file doesn't exist
	timestampDir, _ := ioutil.TempDir("", "timestampDir-test")
	timestampPath := filepath.Join(timestampDir, "gh-update")
	updater := Updater{timestampPath: timestampPath}

	assert.T(t, updater.timeToUpdate())
	timestamp, err := ioutil.ReadFile(timestampPath)
	assert.Equal(t, nil, err)
	assert.NotEqual(t, "", string(timestamp))

	// invalid timestamp format
	timestampFile, _ := ioutil.TempFile("", "timestampFile-test")
	updater = Updater{timestampPath: timestampFile.Name()}
	assert.T(t, updater.timeToUpdate())
	timestamp, err = ioutil.ReadFile(timestampFile.Name())
	assert.Equal(t, nil, err)
	assert.NotEqual(t, "", string(timestamp))

	// dev version
	updater = Updater{CurrentVersion: "dev"}
	assert.T(t, !updater.timeToUpdate())
}
Example #20
0
func TestRootService_One(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("root.json"))
	})

	url, err := RootURL.Expand(nil)
	assert.Equal(t, nil, err)

	root, result := client.Root(url).One()
	assert.T(t, !result.HasError())
	assert.Equal(t, "https://api.github.com/users/{user}", string(root.UserURL))
}
Example #21
0
func TestIssuesService_One(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octocat/Hello-World/issues/1347", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("issue.json"))
	})

	url, err := RepoIssuesURL.Expand(M{"owner": "octocat", "repo": "Hello-World", "number": 1347})
	assert.Equal(t, nil, err)

	issue, result := client.Issues(url).One()

	assert.T(t, !result.HasError())
	validateIssue(t, *issue)
}
Example #22
0
func TestRootEmojisService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("emojis.json"))
	})

	url, err := EmojisURL.Expand(nil)
	assert.Equal(t, nil, err)

	emojis, result := client.Emojis(url).All()
	assert.T(t, !result.HasError())
	var penguin = "https://github.global.ssl.fastly.net/images/icons/emoji/penguin.png?v5"
	var metal = "https://github.global.ssl.fastly.net/images/icons/emoji/metal.png?v5"
	assert.Equal(t, penguin, emojis["penguin"])
	assert.Equal(t, metal, emojis["metal"])
}
Example #23
0
func TestPullRequestService_Patch(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octokit/go-octokit/pulls/1", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		testHeader(t, r, "Accept", patchMediaType)
		respondWith(w, "patches galore")
	})

	url, err := PullRequestsURL.Expand(M{"owner": "octokit", "repo": "go-octokit", "number": 1})
	assert.Equal(t, nil, err)

	patch, result := client.PullRequests(url).Patch()

	assert.T(t, !result.HasError())
	content, err := ioutil.ReadAll(patch)
	assert.Equal(t, nil, err)
	assert.Equal(t, "patches galore", string(content))
}
Example #24
0
func TestUsersService_GetUser(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/users/jingweno", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("user.json"))
	})

	url, err := UserURL.Expand(M{"user": "******"})
	assert.Equal(t, nil, err)
	user, result := client.Users(url).One()

	assert.T(t, !result.HasError())
	assert.Equal(t, 169064, user.ID)
	assert.Equal(t, "jingweno", user.Login)
	assert.Equal(t, "*****@*****.**", user.Email)
	assert.Equal(t, "User", user.Type)
	assert.Equal(t, "https://api.github.com/users/jingweno/repos", string(user.ReposURL))
}
Example #25
0
func TestPullRequestService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/rails/rails/pulls", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		header := w.Header()
		link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="last"`, testURLOf("repositories/8514/pulls?page=2"), testURLOf("repositories/8514/pulls?page=14"))
		header.Set("Link", link)
		respondWithJSON(w, loadFixture("pull_requests.json"))
	})

	url, err := PullRequestsURL.Expand(M{"owner": "rails", "repo": "rails"})
	assert.Equal(t, nil, err)

	prs, result := client.PullRequests(url).All()
	assert.T(t, !result.HasError())
	assert.Equal(t, 30, len(prs))
	assert.Equal(t, testURLStringOf("repositories/8514/pulls?page=2"), string(*result.NextPage))
	assert.Equal(t, testURLStringOf("repositories/8514/pulls?page=14"), string(*result.LastPage))
}
Example #26
0
func TestUsersService_UpdateCurrentUser(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "PUT")
		testBody(t, r, "{\"email\":\"[email protected]\"}\n")
		respondWithJSON(w, loadFixture("user.json"))
	})

	url, _ := CurrentUserURL.Expand(nil)
	userToUpdate := User{Email: "*****@*****.**"}
	user, result := client.Users(url).Update(userToUpdate)

	assert.T(t, !result.HasError())
	assert.Equal(t, 169064, user.ID)
	assert.Equal(t, "jingweno", user.Login)
	assert.Equal(t, "*****@*****.**", user.Email)
	assert.Equal(t, "User", user.Type)
	assert.Equal(t, "https://api.github.com/users/jingweno/repos", string(user.ReposURL))
}
Example #27
0
func TestIssuesService_Update(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octocat/Hello-World/issues/1347", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "PATCH")
		testBody(t, r, "{\"title\":\"title\",\"body\":\"body\"}\n")
		respondWithJSON(w, loadFixture("issue.json"))
	})

	url, err := RepoIssuesURL.Expand(M{"owner": "octocat", "repo": "Hello-World", "number": 1347})
	assert.Equal(t, nil, err)

	params := IssueParams{
		Title: "title",
		Body:  "body",
	}
	issue, result := client.Issues(url).Update(params)

	assert.T(t, !result.HasError())
	validateIssue(t, *issue)
}
Example #28
0
func TestUpdateRelease(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/repos/octokit/Hello-World/releases/123", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "PATCH")
		testBody(t, r, "{\"tag_name\":\"v1.0.0\",\"target_commitish\":\"master\"}\n")
		respondWithJSON(w, loadFixture("create_release.json"))
	})

	url, err := ReleasesURL.Expand(M{"owner": "octokit", "repo": "Hello-World", "id": "123"})
	assert.Equal(t, nil, err)

	params := Release{
		TagName:         "v1.0.0",
		TargetCommitish: "master",
	}
	release, result := client.Releases(url).Update(params)

	assert.T(t, !result.HasError())
	assert.Equal(t, "v1.0.0", release.TagName)
}
Example #29
0
func TestUsersService_GetCurrentUser(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("user.json"))
	})

	url, _ := CurrentUserURL.Expand(nil)
	user, result := client.Users(url).One()

	assert.T(t, !result.HasError())
	assert.Equal(t, 169064, user.ID)
	assert.Equal(t, "jingweno", user.Login)
	assert.Equal(t, "*****@*****.**", user.Email)
	assert.Equal(t, "User", user.Type)
	assert.Equal(t, 17, user.Following)
	assert.Equal(t, 28, user.Followers)
	assert.Equal(t, 90, user.PublicRepos)
	assert.Equal(t, false, user.SiteAdmin)
	assert.Equal(t, "https://api.github.com/users/jingweno/repos", string(user.ReposURL))
}
Example #30
0
func TestEditor_EditTitleAndBodyEmptyTitle(t *testing.T) {
	tempFile, _ := ioutil.TempFile("", "PULLREQ")
	tempFile.Close()

	editor := Editor{
		Program: "memory",
		File:    tempFile.Name(),
		CS:      "#",
		openEditor: func(program string, file string) error {
			assert.Equal(t, "memory", program)
			assert.Equal(t, tempFile.Name(), file)
			return ioutil.WriteFile(file, []byte(""), 0644)
		},
	}

	title, body, err := editor.EditTitleAndBody()
	assert.Equal(t, nil, err)
	assert.Equal(t, "", title)
	assert.Equal(t, "", body)

	_, err = os.Stat(tempFile.Name())
	assert.T(t, os.IsNotExist(err))
}