...

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

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

     1  // Copyright 2022 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  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestEnterpriseService_GetCodeSecurityAndAnalysis(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  
    25  		fmt.Fprint(w, `
    26  		{
    27  		  "advanced_security_enabled_for_new_repositories": true,
    28  		  "secret_scanning_enabled_for_new_repositories": true,
    29  		  "secret_scanning_push_protection_enabled_for_new_repositories": true,
    30  		  "secret_scanning_push_protection_custom_link": "https://github.com/test-org/test-repo/blob/main/README.md"
    31  		}`)
    32  	})
    33  
    34  	ctx := context.Background()
    35  
    36  	const methodName = "GetCodeSecurityAndAnalysis"
    37  
    38  	settings, _, err := client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "e")
    39  	if err != nil {
    40  		t.Errorf("Enterprise.%v returned error: %v", methodName, err)
    41  	}
    42  	want := &EnterpriseSecurityAnalysisSettings{
    43  		AdvancedSecurityEnabledForNewRepositories:             Bool(true),
    44  		SecretScanningEnabledForNewRepositories:               Bool(true),
    45  		SecretScanningPushProtectionEnabledForNewRepositories: Bool(true),
    46  		SecretScanningPushProtectionCustomLink:                String("https://github.com/test-org/test-repo/blob/main/README.md"),
    47  	}
    48  
    49  	if !cmp.Equal(settings, want) {
    50  		t.Errorf("Enterprise.%v return \ngot: %+v,\nwant:%+v", methodName, settings, want)
    51  	}
    52  
    53  	testBadOptions(t, methodName, func() (err error) {
    54  		_, _, err = client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "o")
    55  		return err
    56  	})
    57  
    58  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    59  		got, resp, err := client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "e")
    60  		if got != nil {
    61  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    62  		}
    63  		return resp, err
    64  	})
    65  }
    66  
    67  func TestEnterpriseService_UpdateCodeSecurityAndAnalysis(t *testing.T) {
    68  	client, mux, _, teardown := setup()
    69  	defer teardown()
    70  
    71  	input := &EnterpriseSecurityAnalysisSettings{
    72  		AdvancedSecurityEnabledForNewRepositories:             Bool(true),
    73  		SecretScanningEnabledForNewRepositories:               Bool(true),
    74  		SecretScanningPushProtectionEnabledForNewRepositories: Bool(true),
    75  		SecretScanningPushProtectionCustomLink:                String("https://github.com/test-org/test-repo/blob/main/README.md"),
    76  	}
    77  
    78  	mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(w http.ResponseWriter, r *http.Request) {
    79  		v := new(EnterpriseSecurityAnalysisSettings)
    80  		json.NewDecoder(r.Body).Decode(v)
    81  
    82  		testMethod(t, r, "PATCH")
    83  		if !cmp.Equal(v, input) {
    84  			t.Errorf("Request body = %+v, want %+v", v, input)
    85  		}
    86  	})
    87  
    88  	ctx := context.Background()
    89  
    90  	const methodName = "UpdateCodeSecurityAndAnalysis"
    91  
    92  	_, err := client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "e", input)
    93  	if err != nil {
    94  		t.Errorf("Enterprise.%v returned error: %v", methodName, err)
    95  	}
    96  
    97  	testBadOptions(t, methodName, func() (err error) {
    98  		_, err = client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "o", input)
    99  		return err
   100  	})
   101  
   102  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   103  		return client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "e", input)
   104  	})
   105  }
   106  
   107  func TestEnterpriseService_EnableAdvancedSecurity(t *testing.T) {
   108  	client, mux, _, teardown := setup()
   109  	defer teardown()
   110  
   111  	mux.HandleFunc("/enterprises/e/advanced_security/enable_all", func(w http.ResponseWriter, r *http.Request) {
   112  		testMethod(t, r, "POST")
   113  	})
   114  
   115  	ctx := context.Background()
   116  
   117  	const methodName = "EnableDisableSecurityFeature"
   118  
   119  	_, err := client.Enterprise.EnableDisableSecurityFeature(ctx, "e", "advanced_security", "enable_all")
   120  	if err != nil {
   121  		t.Errorf("Enterprise.%v returned error: %v", methodName, err)
   122  	}
   123  
   124  	testBadOptions(t, methodName, func() (err error) {
   125  		_, err = client.Enterprise.EnableDisableSecurityFeature(ctx, "o", "advanced_security", "enable_all")
   126  		return err
   127  	})
   128  
   129  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   130  		return client.Enterprise.EnableDisableSecurityFeature(ctx, "e", "advanced_security", "enable_all")
   131  	})
   132  }
   133  

View as plain text