...

Source file src/github.com/google/go-github/v55/github/actions_oidc_test.go

Documentation: github.com/google/go-github/v55/github

     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  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestActionsService_GetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		fmt.Fprint(w, `{"include_claim_keys":["repo","context"]}`)
    24  	})
    25  
    26  	ctx := context.Background()
    27  	template, _, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "o")
    28  	if err != nil {
    29  		t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned error: %v", err)
    30  	}
    31  
    32  	want := &OIDCSubjectClaimCustomTemplate{IncludeClaimKeys: []string{"repo", "context"}}
    33  	if !cmp.Equal(template, want) {
    34  		t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned %+v, want %+v", template, want)
    35  	}
    36  
    37  	const methodName = "GetOrgOIDCSubjectClaimCustomTemplate"
    38  	testBadOptions(t, methodName, func() (err error) {
    39  		_, _, err = client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "\n")
    40  		return err
    41  	})
    42  
    43  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    44  		got, resp, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "o")
    45  		if got != nil {
    46  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    47  		}
    48  		return resp, err
    49  	})
    50  }
    51  
    52  func TestActionsService_GetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
    53  	client, mux, _, teardown := setup()
    54  	defer teardown()
    55  
    56  	mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
    57  		testMethod(t, r, "GET")
    58  		fmt.Fprint(w, `{"use_default":false,"include_claim_keys":["repo","context"]}`)
    59  	})
    60  
    61  	ctx := context.Background()
    62  	template, _, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r")
    63  	if err != nil {
    64  		t.Errorf("Actions.GetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
    65  	}
    66  
    67  	want := &OIDCSubjectClaimCustomTemplate{UseDefault: Bool(false), IncludeClaimKeys: []string{"repo", "context"}}
    68  	if !cmp.Equal(template, want) {
    69  		t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned %+v, want %+v", template, want)
    70  	}
    71  
    72  	const methodName = "GetRepoOIDCSubjectClaimCustomTemplate"
    73  	testBadOptions(t, methodName, func() (err error) {
    74  		_, _, err = client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n")
    75  		return err
    76  	})
    77  
    78  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    79  		got, resp, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r")
    80  		if got != nil {
    81  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    82  		}
    83  		return resp, err
    84  	})
    85  }
    86  
    87  func TestActionsService_SetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) {
    88  	client, mux, _, teardown := setup()
    89  	defer teardown()
    90  
    91  	mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
    92  		testMethod(t, r, "PUT")
    93  		testHeader(t, r, "Content-Type", "application/json")
    94  		testBody(t, r, `{"include_claim_keys":["repo","context"]}`+"\n")
    95  		w.WriteHeader(http.StatusCreated)
    96  	})
    97  
    98  	input := &OIDCSubjectClaimCustomTemplate{
    99  		IncludeClaimKeys: []string{"repo", "context"},
   100  	}
   101  	ctx := context.Background()
   102  	_, err := client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "o", input)
   103  	if err != nil {
   104  		t.Errorf("Actions.SetOrgOIDCSubjectClaimCustomTemplate returned error: %v", err)
   105  	}
   106  
   107  	const methodName = "SetOrgOIDCSubjectClaimCustomTemplate"
   108  
   109  	testBadOptions(t, methodName, func() (err error) {
   110  		_, err = client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "\n", input)
   111  		return err
   112  	})
   113  
   114  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   115  		return client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "o", input)
   116  	})
   117  }
   118  
   119  func TestActionsService_SetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
   120  	client, mux, _, teardown := setup()
   121  	defer teardown()
   122  
   123  	mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
   124  		testMethod(t, r, "PUT")
   125  		testHeader(t, r, "Content-Type", "application/json")
   126  		testBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`+"\n")
   127  		w.WriteHeader(http.StatusCreated)
   128  	})
   129  
   130  	input := &OIDCSubjectClaimCustomTemplate{
   131  		UseDefault:       Bool(false),
   132  		IncludeClaimKeys: []string{"repo", "context"},
   133  	}
   134  	ctx := context.Background()
   135  	_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
   136  	if err != nil {
   137  		t.Errorf("Actions.SetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
   138  	}
   139  
   140  	const methodName = "SetRepoOIDCSubjectClaimCustomTemplate"
   141  
   142  	testBadOptions(t, methodName, func() (err error) {
   143  		_, err = client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n", input)
   144  		return err
   145  	})
   146  
   147  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   148  		return client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
   149  	})
   150  }
   151  
   152  func TestActionService_SetRepoOIDCSubjectClaimCustomTemplateToDefault(t *testing.T) {
   153  	client, mux, _, teardown := setup()
   154  	defer teardown()
   155  
   156  	mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
   157  		testMethod(t, r, "PUT")
   158  		testHeader(t, r, "Content-Type", "application/json")
   159  		testBody(t, r, `{"use_default":true}`+"\n")
   160  		w.WriteHeader(http.StatusCreated)
   161  	})
   162  
   163  	input := &OIDCSubjectClaimCustomTemplate{
   164  		UseDefault: Bool(true),
   165  	}
   166  	ctx := context.Background()
   167  	_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
   168  	if err != nil {
   169  		t.Errorf("Actions.SetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
   170  	}
   171  
   172  	const methodName = "SetRepoOIDCSubjectClaimCustomTemplate"
   173  	testBadOptions(t, methodName, func() (err error) {
   174  		_, err = client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n", input)
   175  		return err
   176  	})
   177  
   178  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   179  		return client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
   180  	})
   181  }
   182  

View as plain text