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