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