github.com/google/go-github/v69@v69.2.0/github/enterprise_properties_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  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestEnterpriseService_GetAllCustomProperties(t *testing.T) {
    18  	t.Parallel()
    19  	client, mux, _ := setup(t)
    20  
    21  	mux.HandleFunc("/enterprises/e/properties/schema", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		fmt.Fprint(w, `[
    24  		{
    25            "property_name": "name",
    26            "value_type": "single_select",
    27            "required": true,
    28            "default_value": "production",
    29            "description": "Prod or dev environment",
    30            "allowed_values":[
    31              "production",
    32              "development"
    33            ],
    34            "values_editable_by": "org_actors"
    35          },
    36          {
    37            "property_name": "service",
    38            "value_type": "string"
    39          },
    40          {
    41            "property_name": "team",
    42            "value_type": "string",
    43            "description": "Team owning the repository"
    44          }
    45          ]`)
    46  	})
    47  
    48  	ctx := context.Background()
    49  	properties, _, err := client.Enterprise.GetAllCustomProperties(ctx, "e")
    50  	if err != nil {
    51  		t.Errorf("Enterprise.GetAllCustomProperties returned error: %v", err)
    52  	}
    53  
    54  	want := []*CustomProperty{
    55  		{
    56  			PropertyName:     Ptr("name"),
    57  			ValueType:        "single_select",
    58  			Required:         Ptr(true),
    59  			DefaultValue:     Ptr("production"),
    60  			Description:      Ptr("Prod or dev environment"),
    61  			AllowedValues:    []string{"production", "development"},
    62  			ValuesEditableBy: Ptr("org_actors"),
    63  		},
    64  		{
    65  			PropertyName: Ptr("service"),
    66  			ValueType:    "string",
    67  		},
    68  		{
    69  			PropertyName: Ptr("team"),
    70  			ValueType:    "string",
    71  			Description:  Ptr("Team owning the repository"),
    72  		},
    73  	}
    74  	if !cmp.Equal(properties, want) {
    75  		t.Errorf("Enterprise.GetAllCustomProperties returned %+v, want %+v", properties, want)
    76  	}
    77  
    78  	const methodName = "GetAllCustomProperties"
    79  
    80  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    81  		got, resp, err := client.Enterprise.GetAllCustomProperties(ctx, "e")
    82  		if got != nil {
    83  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    84  		}
    85  		return resp, err
    86  	})
    87  }
    88  
    89  func TestEnterpriseService_CreateOrUpdateCustomProperties(t *testing.T) {
    90  	t.Parallel()
    91  	client, mux, _ := setup(t)
    92  
    93  	mux.HandleFunc("/enterprises/e/properties/schema", func(w http.ResponseWriter, r *http.Request) {
    94  		testMethod(t, r, "PATCH")
    95  		testBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`+"\n")
    96  		fmt.Fprint(w, `[
    97  		{
    98            "property_name": "name",
    99            "value_type": "single_select",
   100            "required": true
   101          },
   102          {
   103            "property_name": "service",
   104            "value_type": "string"
   105          }
   106          ]`)
   107  	})
   108  
   109  	ctx := context.Background()
   110  	properties, _, err := client.Enterprise.CreateOrUpdateCustomProperties(ctx, "e", []*CustomProperty{
   111  		{
   112  			PropertyName: Ptr("name"),
   113  			ValueType:    "single_select",
   114  			Required:     Ptr(true),
   115  		},
   116  		{
   117  			PropertyName: Ptr("service"),
   118  			ValueType:    "string",
   119  		},
   120  	})
   121  	if err != nil {
   122  		t.Errorf("Enterprise.CreateOrUpdateCustomProperties returned error: %v", err)
   123  	}
   124  
   125  	want := []*CustomProperty{
   126  		{
   127  			PropertyName: Ptr("name"),
   128  			ValueType:    "single_select",
   129  			Required:     Ptr(true),
   130  		},
   131  		{
   132  			PropertyName: Ptr("service"),
   133  			ValueType:    "string",
   134  		},
   135  	}
   136  
   137  	if !cmp.Equal(properties, want) {
   138  		t.Errorf("Enterprise.CreateOrUpdateCustomProperties returned %+v, want %+v", properties, want)
   139  	}
   140  
   141  	const methodName = "CreateOrUpdateCustomProperties"
   142  
   143  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   144  		got, resp, err := client.Enterprise.CreateOrUpdateCustomProperties(ctx, "e", nil)
   145  		if got != nil {
   146  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   147  		}
   148  		return resp, err
   149  	})
   150  }
   151  
   152  func TestEnterpriseService_GetCustomProperty(t *testing.T) {
   153  	t.Parallel()
   154  	client, mux, _ := setup(t)
   155  
   156  	mux.HandleFunc("/enterprises/e/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
   157  		testMethod(t, r, "GET")
   158  		fmt.Fprint(w, `{
   159  		"property_name": "name",
   160  		"value_type": "single_select",
   161  		"required": true,
   162  		"default_value": "production",
   163  		"description": "Prod or dev environment",
   164  		"allowed_values":[
   165  		  "production",
   166  		  "development"
   167  		],
   168  		"values_editable_by": "org_actors"
   169  	  }`)
   170  	})
   171  
   172  	ctx := context.Background()
   173  	property, _, err := client.Enterprise.GetCustomProperty(ctx, "e", "name")
   174  	if err != nil {
   175  		t.Errorf("Enterprise.GetCustomProperty returned error: %v", err)
   176  	}
   177  
   178  	want := &CustomProperty{
   179  		PropertyName:     Ptr("name"),
   180  		ValueType:        "single_select",
   181  		Required:         Ptr(true),
   182  		DefaultValue:     Ptr("production"),
   183  		Description:      Ptr("Prod or dev environment"),
   184  		AllowedValues:    []string{"production", "development"},
   185  		ValuesEditableBy: Ptr("org_actors"),
   186  	}
   187  	if !cmp.Equal(property, want) {
   188  		t.Errorf("Enterprise.GetCustomProperty returned %+v, want %+v", property, want)
   189  	}
   190  
   191  	const methodName = "GetCustomProperty"
   192  
   193  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   194  		got, resp, err := client.Enterprise.GetCustomProperty(ctx, "e", "name")
   195  		if got != nil {
   196  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   197  		}
   198  		return resp, err
   199  	})
   200  }
   201  
   202  func TestEnterpriseService_CreateOrUpdateCustomProperty(t *testing.T) {
   203  	t.Parallel()
   204  	client, mux, _ := setup(t)
   205  
   206  	mux.HandleFunc("/enterprises/e/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
   207  		testMethod(t, r, "PUT")
   208  		fmt.Fprint(w, `{
   209  		"property_name": "name",
   210  		"value_type": "single_select",
   211  		"required": true,
   212  		"default_value": "production",
   213  		"description": "Prod or dev environment",
   214  		"allowed_values":[
   215  		  "production",
   216  		  "development"
   217  		],
   218  		"values_editable_by": "org_actors"
   219  	  }`)
   220  	})
   221  
   222  	ctx := context.Background()
   223  	property, _, err := client.Enterprise.CreateOrUpdateCustomProperty(ctx, "e", "name", &CustomProperty{
   224  		ValueType:        "single_select",
   225  		Required:         Ptr(true),
   226  		DefaultValue:     Ptr("production"),
   227  		Description:      Ptr("Prod or dev environment"),
   228  		AllowedValues:    []string{"production", "development"},
   229  		ValuesEditableBy: Ptr("org_actors"),
   230  	})
   231  	if err != nil {
   232  		t.Errorf("Enterprise.CreateOrUpdateCustomProperty returned error: %v", err)
   233  	}
   234  
   235  	want := &CustomProperty{
   236  		PropertyName:     Ptr("name"),
   237  		ValueType:        "single_select",
   238  		Required:         Ptr(true),
   239  		DefaultValue:     Ptr("production"),
   240  		Description:      Ptr("Prod or dev environment"),
   241  		AllowedValues:    []string{"production", "development"},
   242  		ValuesEditableBy: Ptr("org_actors"),
   243  	}
   244  	if !cmp.Equal(property, want) {
   245  		t.Errorf("Enterprise.CreateOrUpdateCustomProperty returned %+v, want %+v", property, want)
   246  	}
   247  
   248  	const methodName = "CreateOrUpdateCustomProperty"
   249  
   250  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   251  		got, resp, err := client.Enterprise.CreateOrUpdateCustomProperty(ctx, "e", "name", nil)
   252  		if got != nil {
   253  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   254  		}
   255  		return resp, err
   256  	})
   257  }
   258  
   259  func TestEnterpriseService_RemoveCustomProperty(t *testing.T) {
   260  	t.Parallel()
   261  	client, mux, _ := setup(t)
   262  
   263  	mux.HandleFunc("/enterprises/e/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
   264  		testMethod(t, r, "DELETE")
   265  	})
   266  
   267  	ctx := context.Background()
   268  	_, err := client.Enterprise.RemoveCustomProperty(ctx, "e", "name")
   269  	if err != nil {
   270  		t.Errorf("Enterprise.RemoveCustomProperty returned error: %v", err)
   271  	}
   272  
   273  	const methodName = "RemoveCustomProperty"
   274  
   275  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   276  		return client.Enterprise.RemoveCustomProperty(ctx, "e", "name")
   277  	})
   278  }