| 1 |
// Copyright 2023 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 |
"reflect" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) { |
| 17 |
client, mux, _, teardown := setup() |
| 18 |
defer teardown() |
| 19 |
|
| 20 |
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) { |
| 21 |
fmt.Fprint(w, `{"total_count":2, "branch_policies":[{"id":1}, {"id": 2}]}`) |
| 22 |
}) |
| 23 |
|
| 24 |
ctx := context.Background() |
| 25 |
got, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e") |
| 26 |
if err != nil { |
| 27 |
t.Errorf("Repositories.ListDeploymentBranchPolicies returned error: %v", err) |
| 28 |
} |
| 29 |
|
| 30 |
want := &DeploymentBranchPolicyResponse{ |
| 31 |
BranchPolicies: []*DeploymentBranchPolicy{ |
| 32 |
{ID: Int64(1)}, |
| 33 |
{ID: Int64(2)}, |
| 34 |
}, |
| 35 |
TotalCount: Int(2), |
| 36 |
} |
| 37 |
if !reflect.DeepEqual(got, want) { |
| 38 |
t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want) |
| 39 |
} |
| 40 |
|
| 41 |
const methodName = "ListDeploymentBranchPolicies" |
| 42 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 43 |
got, resp, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e") |
| 44 |
if got != nil { |
| 45 |
t.Errorf("got non-nil Repositories.ListDeploymentBranchPolicies response: %+v", got) |
| 46 |
} |
| 47 |
return resp, err |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) { |
| 52 |
client, mux, _, teardown := setup() |
| 53 |
defer teardown() |
| 54 |
|
| 55 |
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) { |
| 56 |
fmt.Fprint(w, `{"id":1}`) |
| 57 |
}) |
| 58 |
|
| 59 |
ctx := context.Background() |
| 60 |
got, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1) |
| 61 |
if err != nil { |
| 62 |
t.Errorf("Repositories.GetDeploymentBranchPolicy returned error: %v", err) |
| 63 |
} |
| 64 |
|
| 65 |
want := &DeploymentBranchPolicy{ID: Int64(1)} |
| 66 |
if !reflect.DeepEqual(got, want) { |
| 67 |
t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want) |
| 68 |
} |
| 69 |
|
| 70 |
const methodName = "GetDeploymentBranchPolicy" |
| 71 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 72 |
got, resp, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1) |
| 73 |
if got != nil { |
| 74 |
t.Errorf("got non-nil Repositories.GetDeploymentBranchPolicy response: %+v", got) |
| 75 |
} |
| 76 |
return resp, err |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
func TestRepositoriesService_CreateDeploymentBranchPolicy(t *testing.T) { |
| 81 |
client, mux, _, teardown := setup() |
| 82 |
defer teardown() |
| 83 |
|
| 84 |
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) { |
| 85 |
testMethod(t, r, "POST") |
| 86 |
fmt.Fprint(w, `{"id":1, "type":"branch"}`) |
| 87 |
}) |
| 88 |
|
| 89 |
ctx := context.Background() |
| 90 |
got, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n"), Type: String("branch")}) |
| 91 |
if err != nil { |
| 92 |
t.Errorf("Repositories.CreateDeploymentBranchPolicy returned error: %v", err) |
| 93 |
} |
| 94 |
|
| 95 |
want := &DeploymentBranchPolicy{ID: Int64(1), Type: String("branch")} |
| 96 |
if !reflect.DeepEqual(got, want) { |
| 97 |
t.Errorf("Repositories.CreateDeploymentBranchPolicy = %+v, want %+v", got, want) |
| 98 |
} |
| 99 |
|
| 100 |
const methodName = "CreateDeploymentBranchPolicy" |
| 101 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 102 |
got, resp, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n")}) |
| 103 |
if got != nil { |
| 104 |
t.Errorf("got non-nil Repositories.CreateDeploymentBranchPolicy response: %+v", got) |
| 105 |
} |
| 106 |
return resp, err |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
func TestRepositoriesService_UpdateDeploymentBranchPolicy(t *testing.T) { |
| 111 |
client, mux, _, teardown := setup() |
| 112 |
defer teardown() |
| 113 |
|
| 114 |
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) { |
| 115 |
testMethod(t, r, "PUT") |
| 116 |
fmt.Fprint(w, `{"id":1}`) |
| 117 |
}) |
| 118 |
|
| 119 |
ctx := context.Background() |
| 120 |
got, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")}) |
| 121 |
if err != nil { |
| 122 |
t.Errorf("Repositories.UpdateDeploymentBranchPolicy returned error: %v", err) |
| 123 |
} |
| 124 |
|
| 125 |
want := &DeploymentBranchPolicy{ID: Int64(1)} |
| 126 |
if !reflect.DeepEqual(got, want) { |
| 127 |
t.Errorf("Repositories.UpdateDeploymentBranchPolicy = %+v, want %+v", got, want) |
| 128 |
} |
| 129 |
|
| 130 |
const methodName = "UpdateDeploymentBranchPolicy" |
| 131 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 132 |
got, resp, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")}) |
| 133 |
if got != nil { |
| 134 |
t.Errorf("got non-nil Repositories.UpdateDeploymentBranchPolicy response: %+v", got) |
| 135 |
} |
| 136 |
return resp, err |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func TestRepositoriesService_DeleteDeploymentBranchPolicy(t *testing.T) { |
| 141 |
client, mux, _, teardown := setup() |
| 142 |
defer teardown() |
| 143 |
|
| 144 |
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) { |
| 145 |
testMethod(t, r, "DELETE") |
| 146 |
}) |
| 147 |
|
| 148 |
ctx := context.Background() |
| 149 |
_, err := client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1) |
| 150 |
if err != nil { |
| 151 |
t.Errorf("Repositories.DeleteDeploymentBranchPolicy returned error: %v", err) |
| 152 |
} |
| 153 |
|
| 154 |
const methodName = "DeleteDeploymentBranchPolicy" |
| 155 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 156 |
return client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1) |
| 157 |
}) |
| 158 |
} |