github.com/google/go-github/v60@v60.0.0/github/repos_deployment_protection_rules_test.go (about)

     1  // Copyright 2024 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  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"reflect"
    14  	"testing"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestRepositoriesService_GetAllDeploymentProtectionRules(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		fmt.Fprint(w, `{"total_count":2, "custom_deployment_protection_rules":[{ "id": 3, "node_id": "IEH37kRlcGxveW1lbnRTdGF0ddiv", "enabled": true, "app": { "id": 1, "node_id": "GHT58kRlcGxveW1lbnRTdTY!bbcy", "slug": "a-custom-app", "integration_url": "https://api.github.com/apps/a-custom-app"}}, { "id": 4, "node_id": "MDE2OkRlcGxveW1lbnRTdHJ41128", "enabled": true, "app": { "id": 1, "node_id": "UHVE67RlcGxveW1lbnRTdTY!jfeuy", "slug": "another-custom-app", "integration_url": "https://api.github.com/apps/another-custom-app"}}]}`)
    26  	})
    27  
    28  	ctx := context.Background()
    29  	got, _, err := client.Repositories.GetAllDeploymentProtectionRules(ctx, "o", "r", "e")
    30  	if err != nil {
    31  		t.Errorf("Repositories.GetAllDeploymentProtectionRules returned error: %v", err)
    32  	}
    33  
    34  	want := &ListDeploymentProtectionRuleResponse{
    35  		ProtectionRules: []*CustomDeploymentProtectionRule{
    36  			{ID: Int64(3), NodeID: String("IEH37kRlcGxveW1lbnRTdGF0ddiv"), Enabled: Bool(true), App: &CustomDeploymentProtectionRuleApp{ID: Int64(1), NodeID: String("GHT58kRlcGxveW1lbnRTdTY!bbcy"), Slug: String("a-custom-app"), IntegrationURL: String("https://api.github.com/apps/a-custom-app")}},
    37  			{ID: Int64(4), NodeID: String("MDE2OkRlcGxveW1lbnRTdHJ41128"), Enabled: Bool(true), App: &CustomDeploymentProtectionRuleApp{ID: Int64(1), NodeID: String("UHVE67RlcGxveW1lbnRTdTY!jfeuy"), Slug: String("another-custom-app"), IntegrationURL: String("https://api.github.com/apps/another-custom-app")}},
    38  		},
    39  		TotalCount: Int(2),
    40  	}
    41  	if !reflect.DeepEqual(got, want) {
    42  		t.Errorf("Repositories.GetAllDeploymentProtectionRules = %+v, want %+v", got, want)
    43  	}
    44  
    45  	const methodName = "GetAllDeploymentProtectionRules"
    46  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    47  		got, resp, err := client.Repositories.GetAllDeploymentProtectionRules(ctx, "o", "r", "e")
    48  		if got != nil {
    49  			t.Errorf("got non-nil Repositories.GetAllDeploymentProtectionRules response: %+v", got)
    50  		}
    51  		return resp, err
    52  	})
    53  }
    54  
    55  func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T) {
    56  	client, mux, _, teardown := setup()
    57  	defer teardown()
    58  
    59  	input := &CustomDeploymentProtectionRuleRequest{
    60  		IntegrationID: Int64(5),
    61  	}
    62  
    63  	mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules", func(w http.ResponseWriter, r *http.Request) {
    64  		v := new(CustomDeploymentProtectionRuleRequest)
    65  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
    66  
    67  		testMethod(t, r, "POST")
    68  		want := input
    69  		if !reflect.DeepEqual(v, want) {
    70  			t.Errorf("Request body = %+v, want %+v", v, want)
    71  		}
    72  
    73  		fmt.Fprint(w, `{"id":3, "node_id": "IEH37kRlcGxveW1lbnRTdGF0ddiv", "enabled": true, "app": {"id": 1, "node_id": "GHT58kRlcGxveW1lbnRTdTY!bbcy", "slug": "a-custom-app", "integration_url": "https://api.github.com/apps/a-custom-app"}}`)
    74  	})
    75  
    76  	ctx := context.Background()
    77  	got, _, err := client.Repositories.CreateCustomDeploymentProtectionRule(ctx, "o", "r", "e", input)
    78  	if err != nil {
    79  		t.Errorf("Repositories.CreateCustomDeploymentProtectionRule returned error: %v", err)
    80  	}
    81  
    82  	want := &CustomDeploymentProtectionRule{
    83  		ID:      Int64(3),
    84  		NodeID:  String("IEH37kRlcGxveW1lbnRTdGF0ddiv"),
    85  		Enabled: Bool(true),
    86  		App: &CustomDeploymentProtectionRuleApp{
    87  			ID:             Int64(1),
    88  			NodeID:         String("GHT58kRlcGxveW1lbnRTdTY!bbcy"),
    89  			Slug:           String("a-custom-app"),
    90  			IntegrationURL: String("https://api.github.com/apps/a-custom-app"),
    91  		},
    92  	}
    93  	if !reflect.DeepEqual(got, want) {
    94  		t.Errorf("Repositories.CreateCustomDeploymentProtectionRule = %+v, want %+v", got, want)
    95  	}
    96  
    97  	const methodName = "CreateCustomDeploymentProtectionRule"
    98  	testBadOptions(t, methodName, func() (err error) {
    99  		_, _, err = client.Repositories.CreateCustomDeploymentProtectionRule(ctx, "\n", "\n", "\n", input)
   100  		return err
   101  	})
   102  
   103  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   104  		got, resp, err := client.Repositories.CreateCustomDeploymentProtectionRule(ctx, "o", "r", "e", input)
   105  		if got != nil {
   106  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   107  		}
   108  		return resp, err
   109  	})
   110  }
   111  
   112  func TestRepositoriesService_ListCustomDeploymentRuleIntegrations(t *testing.T) {
   113  	client, mux, _, teardown := setup()
   114  	defer teardown()
   115  
   116  	mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules/apps", func(w http.ResponseWriter, r *http.Request) {
   117  		testMethod(t, r, "GET")
   118  		fmt.Fprint(w, `{"total_count": 2, "available_custom_deployment_protection_rule_integrations": [{"id": 1, "node_id": "GHT58kRlcGxveW1lbnRTdTY!bbcy", "slug": "a-custom-app", "integration_url": "https://api.github.com/apps/a-custom-app"}, {"id": 2, "node_id": "UHVE67RlcGxveW1lbnRTdTY!jfeuy", "slug": "another-custom-app", "integration_url": "https://api.github.com/apps/another-custom-app"}]}`)
   119  	})
   120  
   121  	ctx := context.Background()
   122  	got, _, err := client.Repositories.ListCustomDeploymentRuleIntegrations(ctx, "o", "r", "e")
   123  	if err != nil {
   124  		t.Errorf("Repositories.ListCustomDeploymentRuleIntegrations returned error: %v", err)
   125  	}
   126  
   127  	want := &ListCustomDeploymentRuleIntegrationsResponse{
   128  		TotalCount: Int(2),
   129  		AvailableIntegrations: []*CustomDeploymentProtectionRuleApp{
   130  			{ID: Int64(1), NodeID: String("GHT58kRlcGxveW1lbnRTdTY!bbcy"), Slug: String("a-custom-app"), IntegrationURL: String("https://api.github.com/apps/a-custom-app")},
   131  			{ID: Int64(2), NodeID: String("UHVE67RlcGxveW1lbnRTdTY!jfeuy"), Slug: String("another-custom-app"), IntegrationURL: String("https://api.github.com/apps/another-custom-app")},
   132  		},
   133  	}
   134  	if !reflect.DeepEqual(got, want) {
   135  		t.Errorf("Repositories.ListCustomDeploymentRuleIntegrations = %+v, want %+v", got, want)
   136  	}
   137  
   138  	const methodName = "ListCustomDeploymentRuleIntegrations"
   139  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   140  		got, resp, err := client.Repositories.ListCustomDeploymentRuleIntegrations(ctx, "o", "r", "e")
   141  		if got != nil {
   142  			t.Errorf("got non-nil Repositories.ListCustomDeploymentRuleIntegrations response: %+v", got)
   143  		}
   144  		return resp, err
   145  	})
   146  }
   147  
   148  func TestRepositoriesService_GetCustomDeploymentProtectionRule(t *testing.T) {
   149  	client, mux, _, teardown := setup()
   150  	defer teardown()
   151  
   152  	mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules/1", func(w http.ResponseWriter, r *http.Request) {
   153  		testMethod(t, r, "GET")
   154  		fmt.Fprint(w, `{"id":1, "node_id": "IEH37kRlcGxveW1lbnRTdGF0ddiv", "enabled": true, "app": {"id": 1, "node_id": "GHT58kRlcGxveW1lbnRTdTY!bbcy", "slug": "a-custom-app", "integration_url": "https://api.github.com/apps/a-custom-app"}}`)
   155  	})
   156  
   157  	ctx := context.Background()
   158  	got, _, err := client.Repositories.GetCustomDeploymentProtectionRule(ctx, "o", "r", "e", 1)
   159  	if err != nil {
   160  		t.Errorf("Repositories.GetCustomDeploymentProtectionRule returned error: %v", err)
   161  	}
   162  
   163  	want := &CustomDeploymentProtectionRule{
   164  		ID:      Int64(1),
   165  		NodeID:  String("IEH37kRlcGxveW1lbnRTdGF0ddiv"),
   166  		Enabled: Bool(true),
   167  		App: &CustomDeploymentProtectionRuleApp{
   168  			ID:             Int64(1),
   169  			NodeID:         String("GHT58kRlcGxveW1lbnRTdTY!bbcy"),
   170  			Slug:           String("a-custom-app"),
   171  			IntegrationURL: String("https://api.github.com/apps/a-custom-app"),
   172  		},
   173  	}
   174  
   175  	if !reflect.DeepEqual(got, want) {
   176  		t.Errorf("Repositories.GetCustomDeploymentProtectionRule = %+v, want %+v", got, want)
   177  	}
   178  
   179  	const methodName = "GetCustomDeploymentProtectionRule"
   180  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   181  		got, resp, err := client.Repositories.GetCustomDeploymentProtectionRule(ctx, "o", "r", "e", 1)
   182  		if got != nil {
   183  			t.Errorf("got non-nil Repositories.GetCustomDeploymentProtectionRule response: %+v", got)
   184  		}
   185  		return resp, err
   186  	})
   187  }
   188  
   189  func TestRepositoriesService_DisableCustomDeploymentProtectionRule(t *testing.T) {
   190  	client, mux, _, teardown := setup()
   191  	defer teardown()
   192  
   193  	mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules/1", func(w http.ResponseWriter, r *http.Request) {
   194  		testMethod(t, r, "DELETE")
   195  		w.WriteHeader(http.StatusNoContent)
   196  	})
   197  
   198  	ctx := context.Background()
   199  	resp, err := client.Repositories.DisableCustomDeploymentProtectionRule(ctx, "o", "r", "e", 1)
   200  	if err != nil {
   201  		t.Errorf("Repositories.DisableCustomDeploymentProtectionRule returned error: %v", err)
   202  	}
   203  
   204  	if !cmp.Equal(resp.StatusCode, 204) {
   205  		t.Errorf("Repositories.DisableCustomDeploymentProtectionRule returned  status code %+v, want %+v", resp.StatusCode, "204")
   206  	}
   207  
   208  	const methodName = "DisableCustomDeploymentProtectionRule"
   209  	testBadOptions(t, methodName, func() (err error) {
   210  		_, err = client.Repositories.DisableCustomDeploymentProtectionRule(ctx, "\n", "\n", "\n", 1)
   211  		return err
   212  	})
   213  }