gogs: update endpoint tests

feature/refresh
Brad Rydzewski 6 years ago
parent c033047a00
commit 7016436787

@ -9,60 +9,57 @@ import (
"testing"
"github.com/drone/go-scm/scm"
"github.com/h2non/gock"
)
func testContents(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testContentFind(client))
t.Run("Create", testContentCreate(client))
t.Run("Update", testContentUpdate(client))
t.Run("Delete", testContentDelete(client))
func TestContentFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/raw/f05f642b892d59a0a9ef6a31f6c905a24b5db13a/README.md").
Reply(200).
Type("plain/text").
BodyString("Hello World\n")
client, _ := New("https://try.gogs.io")
result, _, err := client.Contents.Find(
context.Background(),
"gogits/gogs",
"README.md",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
)
if err != nil {
t.Error(err)
}
}
func testContentFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Contents.Find(
context.Background(),
"gogits/gogs",
"README.md",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
)
if err != nil {
t.Error(err)
}
if got, want := result.Path, "README.md"; got != want {
t.Errorf("Want file Path %q, got %q", want, got)
}
if got, want := string(result.Data), "Hello World!\n"; got != want {
t.Errorf("Want file Data %q, got %q", want, got)
}
if got, want := result.Path, "README.md"; got != want {
t.Errorf("Want file Path %q, got %q", want, got)
}
if got, want := string(result.Data), "Hello World\n"; got != want {
t.Errorf("Want file Data %q, got %q", want, got)
}
}
func testContentCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Contents.Create(context.Background(), "gogits/gogs", "README.md", nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestContentCreate(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Contents.Create(context.Background(), "gogits/gogs", "README.md", nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testContentUpdate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Contents.Update(context.Background(), "gogits/gogs", "README.md", nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestContentUpdate(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Contents.Update(context.Background(), "gogits/gogs", "README.md", nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testContentDelete(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Contents.Delete(context.Background(), "gogits/gogs", "README.md", "master")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestContentDelete(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Contents.Delete(context.Background(), "gogits/gogs", "README.md", "master")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

@ -1,5 +0,0 @@
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gogs

@ -1,5 +0,0 @@
GET /api/v1/repos/gogits/gogs/raw/f05f642b892d59a0a9ef6a31f6c905a24b5db13a/README.md
Status: 200
Content-Type: plain/text; charset=UTF-8
Hello World!

@ -1,3 +0,0 @@
DELETE /api/v1/repos/gogits/gogs/hooks/20
Status: 204
Content-Type: application/json; charset=UTF-8

@ -1,19 +0,0 @@
GET /api/v1/repos/gogits/gogs/hooks/20
Status: 200
Content-Type: application/json; charset=UTF-8
{
"id": 20,
"type": "gogs",
"config": {
"content_type": "json",
"url": "http://gogs.io"
},
"events": [
"create",
"push"
],
"active": true,
"updated_at": "2015-08-29T11:31:22.453572732+08:00",
"created_at": "2015-08-29T11:31:22.453569275+08:00"
}

@ -1,13 +0,0 @@
GET /api/v1/orgs/gogits
Status: 200
Content-Type: application/json; charset=UTF-8
{
"id": 1,
"username": "gogits",
"full_name": "gogits",
"avatar_url": "http:\/\/gogits.io\/avatars\/1",
"description": "",
"website": "",
"location": ""
}

@ -1,2 +0,0 @@
GET /api/v1/repos/gogits/go-gogs-client
Status: 404

@ -1,12 +0,0 @@
GET /api/v1/users/janedoe
Status: 200
Content-Type: application/json; charset=UTF-8
{
"id": 1,
"login": "janedoe",
"full_name": "",
"email": "janedoe@gmail.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87",
"username": "janedoe"
}

@ -1,12 +0,0 @@
GET /api/v1/user
Status: 200
Content-Type: application/json; charset=UTF-8
{
"id": 1,
"login": "janedoe",
"full_name": "",
"email": "janedoe@gmail.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87",
"username": "janedoe"
}

@ -98,28 +98,28 @@ func convertBranch(src *branch) *scm.Reference {
}
}
func convertCommitList(src []*commit) []*scm.Commit {
dst := []*scm.Commit{}
for _, v := range src {
dst = append(dst, convertCommit(v))
}
return dst
}
func convertCommit(src *commit) *scm.Commit {
return &scm.Commit{
Sha: src.ID,
Link: src.URL,
Message: src.Message,
Author: convertSignature(src.Author),
Committer: convertSignature(src.Committer),
}
}
func convertSignature(src signature) scm.Signature {
return scm.Signature{
Login: src.Username,
Email: src.Email,
Name: src.Name,
}
}
// func convertCommitList(src []*commit) []*scm.Commit {
// dst := []*scm.Commit{}
// for _, v := range src {
// dst = append(dst, convertCommit(v))
// }
// return dst
// }
// func convertCommit(src *commit) *scm.Commit {
// return &scm.Commit{
// Sha: src.ID,
// Link: src.URL,
// Message: src.Message,
// Author: convertSignature(src.Author),
// Committer: convertSignature(src.Committer),
// }
// }
// func convertSignature(src signature) scm.Signature {
// return scm.Signature{
// Login: src.Username,
// Email: src.Email,
// Name: src.Name,
// }
// }

@ -6,49 +6,44 @@ package gogs
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
)
func testGit(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Branches", testBranches(client))
t.Run("Commits", testCommits(client))
t.Run("Tags", testTags(client))
}
}
//
// commit sub-tests
//
func testCommits(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testCommitFind(client))
t.Run("List", testCommitList(client))
func TestCommitFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.FindCommit(
context.Background(),
"gogits/gogs",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testCommitFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Git.FindCommit(
context.Background(),
"gogits/gogs",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestCommitList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.ListCommits(context.Background(), "gogits/gogs", scm.CommitListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testCommitList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Git.ListCommits(context.Background(), "gogits/gogs", scm.CommitListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestChangeList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.ListChanges(context.Background(), "gogits/gogs", "f05f642b892d59a0a9ef6a31f6c905a24b5db13a", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
@ -56,78 +51,72 @@ func testCommitList(client *scm.Client) func(t *testing.T) {
// branch sub-tests
//
func testBranches(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testBranchFind(client))
t.Run("List", testBranchList(client))
}
}
func TestBranchFind(t *testing.T) {
defer gock.Off()
func testBranchFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Git.FindBranch(context.Background(), "gogits/gogs", "master")
if err != nil {
t.Error(err)
} else {
t.Run("Branch", testBranch(result))
}
}
}
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")
func testBranchList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Git.ListBranches(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != nil {
t.Error(err)
}
if got, want := len(result), 1; got != want {
t.Errorf("Want %d branches, got %d", want, got)
} else {
t.Run("Branch", testBranch(result[0]))
}
client, _ := New("https://try.gogs.io")
got, _, err := client.Git.FindBranch(context.Background(), "gogits/gogs", "master")
if err != nil {
t.Error(err)
}
}
//
// tag sub-tests
//
want := new(scm.Reference)
raw, _ := ioutil.ReadFile("testdata/branch.json.golden")
json.Unmarshal(raw, want)
func testTags(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testTagFind(client))
t.Run("List", testTagList(client))
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testTagFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Git.FindTag(context.Background(), "gogits/gogs", "v1.0.0")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestBranchList(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/branches").
Reply(200).
Type("application/json").
File("testdata/branches.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Git.ListBranches(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != nil {
t.Error(err)
}
}
func testTagList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Git.ListTags(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
want := []*scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/branches.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
//
// struct value sub-tests
// tag sub-tests
//
func testBranch(branch *scm.Reference) func(t *testing.T) {
return func(t *testing.T) {
if got, want := branch.Name, "master"; got != want {
t.Errorf("Want branch Name %q, got %q", want, got)
}
if got, want := branch.Sha, "f05f642b892d59a0a9ef6a31f6c905a24b5db13a"; got != want {
t.Errorf("Want branch Sha %q, got %q", want, got)
}
func TestTagFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.FindTag(context.Background(), "gogits/gogs", "v1.0.0")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func TestTagList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.ListTags(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

@ -5,28 +5,31 @@
// Package gogs implements a Gogs client.
package gogs
import (
"testing"
"github.com/drone/go-scm/scm/driver/gogs/fixtures"
)
import "testing"
func TestClient(t *testing.T) {
server := fixtures.NewServer()
defer server.Close()
client, err := New("https://try.gogs.io")
if err != nil {
t.Error(err)
}
if got, want := client.BaseURL.String(), "https://try.gogs.io/"; got != want {
t.Errorf("Want Client URL %q, got %q", want, got)
}
}
client, err := New(server.URL)
func TestClient_Base(t *testing.T) {
client, err := New("https://try.gogs.io/v1")
if err != nil {
t.Error(err)
return
}
if got, want := client.BaseURL.String(), "https://try.gogs.io/v1/"; got != want {
t.Errorf("Want Client URL %q, got %q", want, got)
}
}
t.Run("Contents", testContents(client))
t.Run("Git", testGit(client))
t.Run("Issues", testIssues(client))
t.Run("Organizations", testOrgs(client))
t.Run("PullRequests", testPullRequests(client))
t.Run("Repositories", testRepos(client))
t.Run("Reviews", testReviews(client))
t.Run("Users", testUsers(client))
func TestClient_Error(t *testing.T) {
_, err := New("http://a b.com/")
if err == nil {
t.Errorf("Expect error when invalid URL")
}
}

@ -6,90 +6,120 @@ package gogs
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
)
func testIssues(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testIssueFind(client))
t.Run("List", testIssueList(client))
t.Run("Create", testIssueCreate(client))
t.Run("Close", testIssueClose(client))
t.Run("Lock", testIssueLock(client))
t.Run("Unlock", testIssueUnlock(client))
t.Run("Comments", testIssueComments(client))
}
}
//
// issue sub-tests
//
func testIssueFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Issues.Find(context.Background(), "gogits/gogs", 1)
if err != nil {
t.Error(err)
} else {
t.Run("Issue", testIssue(result))
}
func TestIssueFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/issues/1").
Reply(200).
Type("application/json").
File("testdata/issue.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Issues.Find(context.Background(), "gogits/gogs", 1)
if err != nil {
t.Error(err)
}
want := new(scm.Issue)
raw, _ := ioutil.ReadFile("testdata/issue.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testIssueList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Issues.List(context.Background(), "gogits/gogs", scm.IssueListOptions{})
if err != nil {
t.Error(err)
} else if got, want := len(result), 1; got != want {
t.Errorf("Want %d issues, got %d", want, got)
} else {
t.Run("Issue", testIssue(result[0]))
}
func TestIssueList(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/issues").
Reply(200).
Type("application/json").
File("testdata/issues.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Issues.List(context.Background(), "gogits/gogs", scm.IssueListOptions{})
if err != nil {
t.Error(err)
}
want := []*scm.Issue{}
raw, _ := ioutil.ReadFile("testdata/issues.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testIssueCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
input := scm.IssueInput{
Title: "Bug found",
Body: "I'm having a problem with this.",
}
result, _, err := client.Issues.Create(context.Background(), "gogits/gogs", &input)
if err != nil {
t.Error(err)
} else {
t.Run("Issue", testIssue(result))
}
func TestIssueCreate(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Post("/api/v1/repos/gogits/gogs/issues").
Reply(200).
Type("application/json").
File("testdata/issue.json")
input := scm.IssueInput{
Title: "Bug found",
Body: "I'm having a problem with this.",
}
client, _ := New("https://try.gogs.io")
got, _, err := client.Issues.Create(context.Background(), "gogits/gogs", &input)
if err != nil {
t.Error(err)
}
want := new(scm.Issue)
raw, _ := ioutil.ReadFile("testdata/issue.json.golden")
json.Unmarshal(raw, want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testIssueClose(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Issues.Close(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestIssueClose(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Issues.Close(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testIssueLock(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Issues.Lock(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestIssueLock(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Issues.Lock(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testIssueUnlock(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Issues.Unlock(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestIssueUnlock(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Issues.Unlock(context.Background(), "gogits/go-gogs-client", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
@ -97,110 +127,79 @@ func testIssueUnlock(client *scm.Client) func(t *testing.T) {
// issue comment sub-tests
//
func testIssueComments(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testIssueCommentFind(client))
t.Run("List", testIssueCommentList(client))
t.Run("Create", testIssueCommentCreate(client))
t.Run("Delete", testIssueCommentDelete(client))
func TestIssueCommentFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Issues.FindComment(context.Background(), "gogits/go-gogs-client", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testIssueCommentFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Issues.FindComment(context.Background(), "gogits/go-gogs-client", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
}
func TestIssueCommentList(t *testing.T) {
defer gock.Off()
func testIssueCommentList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Issues.ListComments(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != nil {
t.Error(err)
} else if got, want := len(result), 1; got != want {
t.Errorf("Want %d comments, got %d", want, got)
} else {
t.Run("Comment", testIssueComment(result[0]))
}
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/issues/1/comments").
Reply(200).
Type("application/json").
File("testdata/comments.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Issues.ListComments(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != nil {
t.Error(err)
}
}
func testIssueCommentCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
input := &scm.CommentInput{Body: "what?"}
result, _, err := client.Issues.CreateComment(context.Background(), "gogits/gogs", 1, input)
if err != nil {
t.Error(err)
} else {
t.Run("Comment", testIssueComment(result))
}
want := []*scm.Comment{}
raw, _ := ioutil.ReadFile("testdata/comments.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testIssueCommentDelete(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Issues.DeleteComment(context.Background(), "gogits/gogs", 1, 1)
if err != nil {
t.Error(err)
}
func TestIssueCommentCreate(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Post("/api/v1/repos/gogits/gogs/issues/1/comments").
Reply(201).
Type("application/json").
File("testdata/comment.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Issues.CreateComment(context.Background(), "gogits/gogs", 1, &scm.CommentInput{Body: "what?"})
if err != nil {
t.Error(err)
}
}
//
// struct value sub-tests
//
want := new(scm.Comment)
raw, _ := ioutil.ReadFile("testdata/comment.json.golden")
json.Unmarshal(raw, want)
func testIssueComment(comment *scm.Comment) func(t *testing.T) {
return func(t *testing.T) {
if got, want := comment.ID, 74; got != want {
t.Errorf("Want issue comment ID %d, got %d", want, got)
}
if got, want := comment.Body, "what?"; got != want {
t.Errorf("Want issue comment Body %q, got %q", want, got)
}
if got, want := comment.Author.Login, "unknwon"; got != want {
t.Errorf("Want issue comment author Login %q, got %q", want, got)
}
if got, want := comment.Author.Avatar, "http://localhost:3000/avatars/1"; got != want {
t.Errorf("Want issue comment author Avatar %q, got %q", want, got)
}
if got, want := comment.Created.Unix(), int64(1472237898); got != want {
t.Errorf("Want issue comment Created %d, got %d", want, got)
}
if got, want := comment.Updated.Unix(), int64(1472237898); got != want {
t.Errorf("Want issue comment Updated %d, got %d", want, got)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
if gock.IsPending() {
t.Errorf("Pending API calls")
}
}
func testIssue(issue *scm.Issue) func(t *testing.T) {
return func(t *testing.T) {
if got, want := issue.Number, 1; got != want {
t.Errorf("Want issue Number %d, got %d", want, got)
}
if got, want := issue.Title, "Bug found"; got != want {
t.Errorf("Want issue Title %q, got %q", want, got)
}
if got, want := issue.Body, "I'm having a problem with this."; got != want {
t.Errorf("Want issue Title %q, got %q", want, got)
}
if got, want := issue.Closed, false; got != want {
t.Errorf("Want issue Title %v, got %v", want, got)
}
if got, want := issue.Author.Login, "janedoe"; got != want {
t.Errorf("Want issue author Login %q, got %q", want, got)
}
if got, want := issue.Author.Avatar, "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"; got != want {
t.Errorf("Want issue author Avatar %q, got %q", want, got)
}
if got, want := issue.Created.Unix(), int64(1506194641); got != want {
t.Errorf("Want issue Created %d, got %d", want, got)
}
if got, want := issue.Updated.Unix(), int64(1506194641); got != want {
t.Errorf("Want issue Created %d, got %d", want, got)
}
func TestIssueCommentDelete(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Delete("/api/v1/repos/gogits/gogs/issues/1/comments/1").
Reply(204).
Type("application/json")
client, _ := New("https://try.gogs.io")
_, err := client.Issues.DeleteComment(context.Background(), "gogits/gogs", 1, 1)
if err != nil {
t.Error(err)
}
}

@ -6,52 +6,62 @@ package gogs
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
)
func testOrgs(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testOrgFind(client))
t.Run("List", testOrgList(client))
func TestOrgFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/orgs/gogits").
Reply(200).
Type("application/json").
File("testdata/organization.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Organizations.Find(context.Background(), "gogits")
if err != nil {
t.Error(err)
}
}
func testOrgFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Organizations.Find(context.Background(), "gogits")
if err != nil {
t.Error(err)
}
t.Run("Organization", testOrganization(result))
want := new(scm.Organization)
raw, _ := ioutil.ReadFile("testdata/organization.json.golden")
json.Unmarshal(raw, want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testOrgList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Organizations.List(
context.Background(),
scm.ListOptions{},
)
if err != nil {
t.Error(err)
}
if got, want := len(result), 1; got != want {
t.Errorf("Want %d organizations, got %d", want, got)
} else {
t.Run("Organization", testOrganization(result[0]))
}
func TestOrgList(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/user/orgs").
Reply(200).
Type("application/json").
File("testdata/organizations.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Organizations.List(context.Background(), scm.ListOptions{})
if err != nil {
t.Error(err)
}
}
func testOrganization(organization *scm.Organization) func(t *testing.T) {
return func(t *testing.T) {
if got, want := organization.Name, "gogits"; got != want {
t.Errorf("Want organization Name %q, got %q", want, got)
}
if got, want := organization.Avatar, "http://gogits.io/avatars/1"; got != want {
t.Errorf("Want organization Avatar %q, got %q", want, got)
}
want := []*scm.Organization{}
raw, _ := ioutil.ReadFile("testdata/organizations.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

@ -11,54 +11,39 @@ import (
"github.com/drone/go-scm/scm"
)
func testPullRequests(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testPullRequestFind(client))
t.Run("List", testPullRequestList(client))
t.Run("Close", testPullRequestClose(client))
t.Run("Merge", testPullRequestMerge(client))
t.Run("Changes", testPullRequestChanges(client))
t.Run("Comments", testPullRequestComments(client))
}
}
//
// pull request sub-tests
//
func testPullRequestFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.Find(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.Find(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.List(context.Background(), "gogits/gogs", scm.PullRequestListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.List(context.Background(), "gogits/gogs", scm.PullRequestListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestClose(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.PullRequests.Close(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestClose(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.PullRequests.Close(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestMerge(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.PullRequests.Merge(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestMerge(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.PullRequests.Merge(context.Background(), "gogits/gogs", 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
@ -66,12 +51,11 @@ func testPullRequestMerge(client *scm.Client) func(t *testing.T) {
// pull request change sub-tests
//
func testPullRequestChanges(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.ListChanges(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestChanges(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.ListChanges(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
@ -79,47 +63,34 @@ func testPullRequestChanges(client *scm.Client) func(t *testing.T) {
// pull request comment sub-tests
//
func testPullRequestComments(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testPullRequestCommentFind(client))
t.Run("List", testPullRequestCommentList(client))
t.Run("Create", testPullRequestCommentCreate(client))
t.Run("Delete", testPullRequestCommentDelete(client))
}
}
func testPullRequestCommentFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.FindComment(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestCommentFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.FindComment(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestCommentList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.ListComments(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestCommentList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.ListComments(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestCommentCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.PullRequests.CreateComment(context.Background(), "gogits/gogs", 1, &scm.CommentInput{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestCommentCreate(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.PullRequests.CreateComment(context.Background(), "gogits/gogs", 1, &scm.CommentInput{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPullRequestCommentDelete(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.PullRequests.DeleteComment(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestPullRequestCommentDelete(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.PullRequests.DeleteComment(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

@ -6,71 +6,108 @@ package gogs
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
)
func testRepos(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testRepoFind(client))
t.Run("FindPerm", testRepoFindPerm(client))
t.Run("NotFound", testRepoNotFound(client))
t.Run("List", testRepoList(client))
t.Run("Hooks", testHooks(client))
t.Run("Statuses", testStatuses(client))
}
}
//
// repository sub-tests
//
func testRepoFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.Find(context.Background(), "gogits/gogs")
if err != nil {
t.Error(err)
} else {
t.Run("Repository", testRepository(result))
t.Run("Permissions", testPermissions(result.Perm))
}
func TestRepoFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs").
Reply(200).
Type("application/json").
File("testdata/repo.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.Find(context.Background(), "gogits/gogs")
if err != nil {
t.Error(err)
}
want := new(scm.Repository)
raw, _ := ioutil.ReadFile("testdata/repo.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testRepoFindPerm(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.FindPerms(context.Background(), "gogits/gogs")
if err != nil {
t.Error(err)
} else {
t.Run("Permissions", testPermissions(result))
}
func TestRepoFindPerm(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs").
Reply(200).
Type("application/json").
File("testdata/repo.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.FindPerms(context.Background(), "gogits/gogs")
if err != nil {
t.Error(err)
}
want := new(scm.Repository)
raw, _ := ioutil.ReadFile("testdata/repo.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want.Perm); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testRepoList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.List(context.Background(), scm.ListOptions{})
if err != nil {
t.Error(err)
} else if got, want := len(result), 1; got != want {
t.Errorf("Want %d repositories, got %d", want, got)
} else {
t.Run("Repository", testRepository(result[0]))
t.Run("Permissions", testPermissions(result[0].Perm))
}
func TestRepoList(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/user/repos").
Reply(200).
Type("application/json").
File("testdata/repos.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.List(context.Background(), scm.ListOptions{})
if err != nil {
t.Error(err)
}
want := []*scm.Repository{}
raw, _ := ioutil.ReadFile("testdata/repos.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testRepoNotFound(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Repositories.FindPerms(context.Background(), "gogits/go-gogs-client")
if err == nil {
t.Errorf("Expect Not Found error")
} else if got, want := err.Error(), "Not Found"; got != want {
t.Errorf("Want error %q, got %q", want, got)
}
func TestRepoNotFound(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/go-gogs-client").
Reply(404).
Type("text/plain")
client, _ := New("https://try.gogs.io")
_, _, err := client.Repositories.FindPerms(context.Background(), "gogits/go-gogs-client")
if err == nil {
t.Errorf("Expect Not Found error")
} else if got, want := err.Error(), "Not Found"; got != want {
t.Errorf("Want error %q, got %q", want, got)
}
}
@ -78,136 +115,164 @@ func testRepoNotFound(client *scm.Client) func(t *testing.T) {
// hook sub-tests
//
func testHooks(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testHookFind(client))
t.Run("List", testHookList(client))
t.Run("Create", testHookCreate(client))
t.Run("Delete", testHookDelete(client))
func TestHookFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/hooks/20").
Reply(200).
Type("application/json").
File("testdata/hook.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.FindHook(context.Background(), "gogits/gogs", "20")
if err != nil {
t.Error(err)
}
}
func testHookFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.FindHook(context.Background(), "gogits/gogs", "20")
if err != nil {
t.Error(err)
} else {
t.Run("Hook", testHook(result))
}
want := new(scm.Hook)
raw, _ := ioutil.ReadFile("testdata/hook.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testHookList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.ListHooks(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != nil {
t.Error(err)
} else if got, want := len(result), 1; got != want {
t.Errorf("Want %d hooks, got %d", want, got)
} else {
t.Run("Hook", testHook(result[0]))
}
func TestHookList(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/hooks").
Reply(200).
Type("application/json").
File("testdata/hooks.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.ListHooks(context.Background(), "gogits/gogs", scm.ListOptions{})
if err != nil {
t.Error(err)
}
}
func testHookCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Repositories.CreateHook(context.Background(), "gogits/gogs", &scm.HookInput{})
if err != nil {
t.Error(err)
} else {
t.Run("Hook", testHook(result))
}
want := []*scm.Hook{}
raw, _ := ioutil.ReadFile("testdata/hooks.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testHookDelete(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Repositories.DeleteHook(context.Background(), "gogits/gogs", "20")
if err != nil {
t.Error(err)
}
func TestHookCreate(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Post("/api/v1/repos/gogits/gogs/hooks").
Reply(201).
Type("application/json").
File("testdata/hook.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Repositories.CreateHook(context.Background(), "gogits/gogs", &scm.HookInput{})
if err != nil {
t.Error(err)
}
}
//
// status sub-tests
//
want := new(scm.Hook)
raw, _ := ioutil.ReadFile("testdata/hook.json.golden")
json.Unmarshal(raw, &want)
func testStatuses(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("List", testStatusList(client))
t.Run("Create", testStatusCreate(client))
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testStatusList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Repositories.ListStatus(context.Background(), "gogits/gogs", "master", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestHookDelete(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Delete("/api/v1/repos/gogits/gogs/hooks/20").
Reply(204).
Type("application/json")
client, _ := New("https://try.gogs.io")
_, err := client.Repositories.DeleteHook(context.Background(), "gogits/gogs", "20")
if err != nil {
t.Error(err)
}
}
func testStatusCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Repositories.CreateStatus(context.Background(), "gogits/gogs", "master", &scm.StatusInput{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
func TestHookEvents(t *testing.T) {
tests := []struct {
in scm.HookEvents
out []string
}{
{
in: scm.HookEvents{Push: true},
out: []string{"push"},
},
{
in: scm.HookEvents{Branch: true},
out: []string{"create", "delete"},
},
{
in: scm.HookEvents{IssueComment: true},
out: []string{"issue_comment"},
},
{
in: scm.HookEvents{PullRequestComment: true},
out: []string{"issue_comment"},
},
{
in: scm.HookEvents{Issue: true},
out: []string{"issues"},
},
{
in: scm.HookEvents{PullRequest: true},
out: []string{"pull_request"},
},
{
in: scm.HookEvents{
Branch: true,
Issue: true,
IssueComment: true,
PullRequest: true,
PullRequestComment: true,
Push: true,
ReviewComment: true,
Tag: true,
},
out: []string{"pull_request", "issues", "issue_comment", "create", "delete", "push"},
},
}
for _, test := range tests {
got, want := convertHookEvent(test.in), test.out
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
}
//
// struct value sub-tests
// status sub-tests
//
func testRepository(repository *scm.Repository) func(t *testing.T) {
return func(t *testing.T) {
if got, want := repository.ID, "1"; got != want {
t.Errorf("Want repository ID %v, got %v", want, got)
}
if got, want := repository.Name, "gogs"; got != want {
t.Errorf("Want repository Name %q, got %q", want, got)
}
if got, want := repository.Namespace, "gogits"; got != want {
t.Errorf("Want repository Owner %q, got %q", want, got)
}
if got, want := repository.Branch, "master"; got != want {
t.Errorf("Want repository Branch %q, got %q", want, got)
}
if got, want := repository.Private, true; got != want {
t.Errorf("Want repository Private %v, got %v", want, got)
}
func TestStatusList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Repositories.ListStatus(context.Background(), "gogits/gogs", "master", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testPermissions(perms *scm.Perm) func(t *testing.T) {
return func(t *testing.T) {
if got, want := perms.Pull, true; got != want {
t.Errorf("Want permission Pull %v, got %v", want, got)
}
if got, want := perms.Push, true; got != want {
t.Errorf("Want permission Push %v, got %v", want, got)
}
if got, want := perms.Admin, true; got != want {
t.Errorf("Want permission Admin %v, got %v", want, got)
}
}
}
func testHook(hook *scm.Hook) func(t *testing.T) {
return func(t *testing.T) {
if got, want := hook.ID, "20"; got != want {
t.Errorf("Want hook ID %v, got %v", want, got)
}
if got, want := hook.Active, true; got != want {
t.Errorf("Want hook Active %v, got %v", want, got)
}
if got, want := hook.Target, "http://gogs.io"; got != want {
t.Errorf("Want hook Target %v, got %v", want, got)
}
func TestStatusCreate(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Repositories.CreateStatus(context.Background(), "gogits/gogs", "master", &scm.StatusInput{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

@ -11,47 +11,34 @@ import (
"github.com/drone/go-scm/scm"
)
func testReviews(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testReviewFind(client))
t.Run("List", testReviewList(client))
t.Run("Create", testReviewCreate(client))
t.Run("Delete", testReviewDelete(client))
func TestReviewFind(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Reviews.Find(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testReviewFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Reviews.Find(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestReviewList(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Reviews.List(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testReviewList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Reviews.List(context.Background(), "gogits/gogs", 1, scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestReviewCreate(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Reviews.Create(context.Background(), "gogits/gogs", 1, nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func testReviewCreate(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, _, err := client.Reviews.Create(context.Background(), "gogits/gogs", 1, nil)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
}
func testReviewDelete(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
_, err := client.Reviews.Delete(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
func TestReviewDelete(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, err := client.Reviews.Delete(context.Background(), "gogits/gogs", 1, 1)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

@ -1,7 +1,3 @@
GET /api/v1/repos/gogits/gogs/branches/master
Status: 200
Content-Type: application/json; charset=UTF-8
{
"name": "master",
"commit": {

@ -0,0 +1,4 @@
{
"Name": "master",
"Sha": "f05f642b892d59a0a9ef6a31f6c905a24b5db13a"
}

@ -1,7 +1,3 @@
GET /api/v1/repos/gogits/gogs/branches
Status: 200
Content-Type: application/json; charset=UTF-8
[
{
"name": "master",

@ -0,0 +1,6 @@
[
{
"Name": "master",
"Sha": "f05f642b892d59a0a9ef6a31f6c905a24b5db13a"
}
]

@ -0,0 +1,13 @@
{
"id": 74,
"user": {
"id": 1,
"username": "unknwon",
"full_name": "无闻",
"email": "u@gogs.io",
"avatar_url": "http://localhost:3000/avatars/1"
},
"body": "what?",
"created_at": "2016-08-26T11:58:18-07:00",
"updated_at": "2016-08-26T11:58:18-07:00"
}

@ -0,0 +1,12 @@
{
"ID": 74,
"Body": "what?",
"Author": {
"Login": "unknwon",
"Name": "无闻",
"Email": "u@gogs.io",
"Avatar": "http://localhost:3000/avatars/1"
},
"Created": "2016-08-26T11:58:18-07:00",
"Updated": "2016-08-26T11:58:18-07:00"
}

@ -0,0 +1,15 @@
[
{
"id": 74,
"user": {
"id": 1,
"username": "unknwon",
"full_name": "无闻",
"email": "u@gogs.io",
"avatar_url": "http://localhost:3000/avatars/1"
},
"body": "what?",
"created_at": "2016-08-26T11:58:18-07:00",
"updated_at": "2016-08-26T11:58:18-07:00"
}
]

@ -0,0 +1,14 @@
[
{
"ID": 74,
"Body": "what?",
"Author": {
"Login": "unknwon",
"Name": "无闻",
"Email": "u@gogs.io",
"Avatar": "http://localhost:3000/avatars/1"
},
"Created": "2016-08-26T11:58:18-07:00",
"Updated": "2016-08-26T11:58:18-07:00"
}
]

@ -1,7 +1,3 @@
POST /api/v1/repos/gogits/gogs/hooks
Status: 201
Content-Type: application/json; charset=UTF-8
{
"id": 20,
"type": "gogs",

@ -0,0 +1,11 @@
{
"ID": "20",
"Name": "",
"Target": "http://gogs.io",
"Events": [
"create",
"push"
],
"Active": true,
"SkipVerify": false
}

@ -1,14 +1,10 @@
GET /api/v1/repos/gogits/gogs/hooks
Status: 200
Content-Type: application/json; charset=UTF-8
[
{
"id": 20,
"type": "gogs",
"config": {
"content_type": "json",
"url": "http:\/\/gogs.io"
"url": "http://gogs.io"
},
"events": [
"create",

@ -0,0 +1,13 @@
[
{
"ID": "20",
"Name": "",
"Target": "http://gogs.io",
"Events": [
"create",
"push"
],
"Active": true,
"SkipVerify": false
}
]

@ -0,0 +1,25 @@
{
"id": 1,
"number": 1,
"user": {
"id": 1,
"login": "janedoe",
"full_name": "",
"email": "janedoe@mail.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87",
"username": "janedoe"
},
"title": "Bug found",
"body": "I'm having a problem with this.",
"labels": [
],
"milestone": null,
"assignee": null,
"state": "open",
"comments": 0,
"created_at": "2017-09-23T19:24:01Z",
"updated_at": "2017-09-23T19:24:01Z",
"pull_request": null
}

@ -0,0 +1,17 @@
{
"Number": 1,
"Title": "Bug found",
"Body": "I'm having a problem with this.",
"Link": "",
"Labels": null,
"Closed": false,
"Locked": false,
"Author": {
"Login": "janedoe",
"Name": "",
"Email": "janedoe@mail.com",
"Avatar": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"Created": "2017-09-23T19:24:01Z",
"Updated": "2017-09-23T19:24:01Z"
}

@ -0,0 +1,26 @@
[
{
"id": 1,
"number": 1,
"user": {
"id": 1,
"login": "janedoe",
"full_name": "",
"email": "janedoe@mail.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87",
"username": "janedoe"
},
"title": "Bug found",
"body": "I'm having a problem with this.",
"labels": [
],
"milestone": null,
"assignee": null,
"state": "open",
"comments": 0,
"created_at": "2017-09-23T19:24:01Z",
"updated_at": "2017-09-23T19:24:01Z",
"pull_request": null
}
]

@ -0,0 +1,19 @@
[
{
"Number": 1,
"Title": "Bug found",
"Body": "I'm having a problem with this.",
"Link": "",
"Labels": null,
"Closed": false,
"Locked": false,
"Author": {
"Login": "janedoe",
"Name": "",
"Email": "janedoe@mail.com",
"Avatar": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"Created": "2017-09-23T19:24:01Z",
"Updated": "2017-09-23T19:24:01Z"
}
]

@ -0,0 +1,9 @@
{
"id": 1,
"username": "gogits",
"full_name": "gogits",
"avatar_url": "http://gogits.io/avatars/1",
"description": "",
"website": "",
"location": ""
}

@ -0,0 +1,4 @@
{
"Name": "gogits",
"Avatar": "http://gogits.io/avatars/1"
}

@ -1,13 +1,9 @@
GET /api/v1/user/orgs
Status: 200
Content-Type: application/json; charset=UTF-8
[
{
"id": 1,
"username": "gogits",
"full_name": "gogits",
"avatar_url": "http:\/\/gogits.io\/avatars\/1",
"avatar_url": "http://gogits.io/avatars/1",
"description": "",
"website": "",
"location": ""

@ -0,0 +1,6 @@
[
{
"Name": "gogits",
"Avatar": "http://gogits.io/avatars/1"
}
]

@ -1,7 +1,3 @@
GET /api/v1/repos/gogits/gogs
Status: 200
Content-Type: application/json; charset=UTF-8
{
"id": 1,
"owner": {
@ -9,11 +5,11 @@ Content-Type: application/json; charset=UTF-8
"login": "gogits",
"full_name": "gogits",
"email": "",
"avatar_url": "http:\/\/gogs.io\/avatars\/1",
"avatar_url": "http://gogs.io/avatars/1",
"username": "gogits"
},
"name": "gogs",
"full_name": "gogits\/gogs",
"full_name": "gogits/gogs",
"description": "",
"private": true,
"fork": false,
@ -21,9 +17,9 @@ Content-Type: application/json; charset=UTF-8
"empty": false,
"mirror": false,
"size": 4485120,
"html_url": "http:\/\/gogs.io\/drone\/cover",
"ssh_url": "git@localhost:drone\/cover.git",
"clone_url": "http:\/\/gogs.io\/drone\/cover.git",
"html_url": "http://gogs.io/drone/cover",
"ssh_url": "git@localhost:drone/cover.git",
"clone_url": "http://gogs.io/drone/cover.git",
"website": "",
"stars_count": 0,
"forks_count": 0,

@ -0,0 +1,17 @@
{
"ID": "1",
"Namespace": "gogits",
"Name": "gogs",
"Perm": {
"Pull": true,
"Push": true,
"Admin": true
},
"Branch": "master",
"Private": true,
"Clone": "http://gogs.io/drone/cover.git",
"CloneSSH": "git@localhost:drone/cover.git",
"Link": "",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
}

@ -1,7 +1,3 @@
GET /api/v1/user/repos
Status: 200
Content-Type: application/json; charset=UTF-8
[
{
"id": 1,
@ -10,11 +6,11 @@ Content-Type: application/json; charset=UTF-8
"login": "gogits",
"full_name": "gogits",
"email": "",
"avatar_url": "http:\/\/gogs.io\/avatars\/1",
"avatar_url": "http://gogs.io/avatars/1",
"username": "gogits"
},
"name": "gogs",
"full_name": "gogits\/gogs",
"full_name": "gogits/gogs",
"description": "",
"private": true,
"fork": false,
@ -22,9 +18,9 @@ Content-Type: application/json; charset=UTF-8
"empty": false,
"mirror": false,
"size": 4485120,
"html_url": "http:\/\/gogs.io\/drone\/cover",
"ssh_url": "git@localhost:drone\/cover.git",
"clone_url": "http:\/\/gogs.io\/drone\/cover.git",
"html_url": "http://gogs.io/drone/cover",
"ssh_url": "git@localhost:drone/cover.git",
"clone_url": "http://gogs.io/drone/cover.git",
"website": "",
"stars_count": 0,
"forks_count": 0,

@ -0,0 +1,19 @@
[
{
"ID": "1",
"Namespace": "gogits",
"Name": "gogs",
"Perm": {
"Pull": true,
"Push": true,
"Admin": true
},
"Branch": "master",
"Private": true,
"Clone": "http://gogs.io/drone/cover.git",
"CloneSSH": "git@localhost:drone/cover.git",
"Link": "",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
}
]

@ -0,0 +1,8 @@
{
"id": 1,
"login": "jcitizen",
"full_name": "Jane Citizen",
"email": "jane@example.com",
"avatar_url": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"username": "jcitizen"
}

@ -0,0 +1,6 @@
{
"Login": "jcitizen",
"Name": "Jane Citizen",
"Email": "jane@example.com",
"Avatar": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
}

@ -6,51 +6,82 @@ package gogs
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
)
func testUsers(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Find", testUserFind(client))
t.Run("FindLogin", testUserFindLogin(client))
t.Run("FindEmail", testUserFindEmail(client))
func TestUserFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/user").
Reply(200).
Type("application/json").
File("testdata/user.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Users.Find(context.Background())
if err != nil {
t.Error(err)
}
}
func testUserFind(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Users.Find(context.Background())
if err != nil {
t.Error(err)
}
if got, want := result.Login, "janedoe"; got != want {
t.Errorf("Want user Login %q, got %q", want, got)
}
want := new(scm.User)
raw, _ := ioutil.ReadFile("testdata/user.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testUserFindLogin(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Users.FindLogin(context.Background(), "janedoe")
if err != nil {
t.Error(err)
}
if got, want := result.Login, "janedoe"; got != want {
t.Errorf("Want user Login %q, got %q", want, got)
}
func TestUserLoginFind(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/users/jcitizen").
Reply(200).
Type("application/json").
File("testdata/user.json")
client, _ := New("https://try.gogs.io")
got, _, err := client.Users.FindLogin(context.Background(), "jcitizen")
if err != nil {
t.Error(err)
}
want := new(scm.User)
raw, _ := ioutil.ReadFile("testdata/user.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func testUserFindEmail(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
result, _, err := client.Users.FindEmail(context.Background())
if err != nil {
t.Error(err)
}
if got, want := result, "janedoe@gmail.com"; got != want {
t.Errorf("Want user Email %q, got %q", want, got)
}
func TestUserFindEmail(t *testing.T) {
defer gock.Off()
gock.New("https://try.gogs.io").
Get("/api/v1/user").
Reply(200).
Type("application/json").
File("testdata/user.json")
client, _ := New("https://try.gogs.io")
email, _, err := client.Users.FindEmail(context.Background())
if err != nil {
t.Error(err)
}
if got, want := email, "jane@example.com"; got != want {
t.Errorf("Want email %s, got %s", want, got)
}
}

@ -47,9 +47,6 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (interface{
if err != nil {
return nil, err
}
if hook == nil {
return nil, nil
}
// get the gogs signature key to verify the payload
// signature. If no key is provided, no validation

@ -137,7 +137,7 @@ func TestWebhooks(t *testing.T) {
s := new(webhookService)
o, err := s.Parse(r, secretFunc)
if err != nil {
if err != nil && err != scm.ErrSignatureInvalid {
t.Error(err)
continue
}
@ -155,6 +155,17 @@ func TestWebhooks(t *testing.T) {
}
}
func TestWebhook_ErrUnknownEvent(t *testing.T) {
f, _ := ioutil.ReadFile("samples/pull_request_edited.json")
r, _ := http.NewRequest("GET", "/", bytes.NewBuffer(f))
s := new(webhookService)
_, err := s.Parse(r, secretFunc)
if err != scm.ErrUnknownEvent {
t.Errorf("Expect unknown event error, got %v", err)
}
}
func TestWebhookInvalid(t *testing.T) {
f, _ := ioutil.ReadFile("samples/pull_request_edited.json")
r, _ := http.NewRequest("GET", "/", bytes.NewBuffer(f))
@ -169,6 +180,33 @@ func TestWebhookInvalid(t *testing.T) {
}
}
func TestWebhookValidated(t *testing.T) {
f, _ := ioutil.ReadFile("samples/pull_request_edited.json")
r, _ := http.NewRequest("GET", "/", bytes.NewBuffer(f))
r.Header.Set("X-Gogs-Event", "pull_request")
r.Header.Set("X-Gogs-Delivery", "ee8d97b4-1479-43f1-9cac-fbbd1b80da55")
r.Header.Set("X-Gogs-Signature", "261d7f124e251f7b2ac309e4b710f3a8cb588076bdec8aa57b3ec8586cc4e790")
s := new(webhookService)
_, err := s.Parse(r, secretFunc)
if err != nil {
t.Errorf("Expect valid signature, got %v", err)
}
}
func TestWebhookMissingSignature(t *testing.T) {
f, _ := ioutil.ReadFile("samples/pull_request_edited.json")
r, _ := http.NewRequest("GET", "/", bytes.NewBuffer(f))
r.Header.Set("X-Gogs-Event", "pull_request")
r.Header.Set("X-Gogs-Delivery", "ee8d97b4-1479-43f1-9cac-fbbd1b80da55")
s := new(webhookService)
_, err := s.Parse(r, secretFunc)
if err != scm.ErrSignatureInvalid {
t.Errorf("Expect invalid signature error, got %v", err)
}
}
func secretFunc(interface{}) (string, error) {
return "root", nil
}

Loading…
Cancel
Save