1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2016 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRepositoriesService_ListInvitations(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/invitations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"page": "2"})
fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
})
opt := &ListOptions{Page: 2}
ctx := context.Background()
got, _, err := client.Repositories.ListInvitations(ctx, "o", "r", opt)
if err != nil {
t.Errorf("Repositories.ListInvitations returned error: %v", err)
}
want := []*RepositoryInvitation{{ID: Int64(1)}, {ID: Int64(2)}}
if !cmp.Equal(got, want) {
t.Errorf("Repositories.ListInvitations = %+v, want %+v", got, want)
}
const methodName = "ListInvitations"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.ListInvitations(ctx, "\n", "\n", opt)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.ListInvitations(ctx, "o", "r", opt)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestRepositoriesService_DeleteInvitation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/invitations/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})
ctx := context.Background()
_, err := client.Repositories.DeleteInvitation(ctx, "o", "r", 2)
if err != nil {
t.Errorf("Repositories.DeleteInvitation returned error: %v", err)
}
const methodName = "DeleteInvitation"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Repositories.DeleteInvitation(ctx, "\n", "\n", 2)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.DeleteInvitation(ctx, "o", "r", 2)
})
}
func TestRepositoriesService_UpdateInvitation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/invitations/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
fmt.Fprintf(w, `{"id":1}`)
})
ctx := context.Background()
got, _, err := client.Repositories.UpdateInvitation(ctx, "o", "r", 2, "write")
if err != nil {
t.Errorf("Repositories.UpdateInvitation returned error: %v", err)
}
want := &RepositoryInvitation{ID: Int64(1)}
if !cmp.Equal(got, want) {
t.Errorf("Repositories.UpdateInvitation = %+v, want %+v", got, want)
}
const methodName = "UpdateInvitation"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.UpdateInvitation(ctx, "\n", "\n", 2, "write")
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.UpdateInvitation(ctx, "o", "r", 2, "write")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestRepositoryInvitation_Marshal(t *testing.T) {
testJSONMarshal(t, &RepositoryInvitation{}, "{}")
r := &RepositoryInvitation{
ID: Int64(1),
Repo: &Repository{
ID: Int64(1),
Name: String("n"),
URL: String("u"),
},
Invitee: &User{
ID: Int64(1),
Name: String("n"),
URL: String("u"),
},
Inviter: &User{
ID: Int64(1),
Name: String("n"),
URL: String("u"),
},
Permissions: String("p"),
CreatedAt: &Timestamp{referenceTime},
URL: String("u"),
HTMLURL: String("h"),
}
want := `{
"id":1,
"repository":{
"id":1,
"name":"n",
"url":"u"
},
"invitee":{
"id":1,
"name":"n",
"url":"u"
},
"inviter":{
"id":1,
"name":"n",
"url":"u"
},
"permissions":"p",
"created_at":` + referenceTimeStr + `,
"url":"u",
"html_url":"h"
}`
testJSONMarshal(t, r, want)
}
|