...

Source file src/github.com/google/go-github/v45/github/secret_scanning_test.go

Documentation: github.com/google/go-github/v45/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  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestSecretScanningService_ListAlertsForEnterprise(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/enterprises/e/secret-scanning/alerts", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{"state": "open", "secret_type": "mailchimp_api_key"})
    26  
    27  		fmt.Fprint(w, `[{
    28  			"number": 1,
    29  			"created_at": "1996-06-20T00:00:00Z",
    30  			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
    31  			"html_url": "https://github.com/o/r/security/secret-scanning/1",
    32  			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
    33  			"state": "open",
    34  			"resolution": null,
    35  			"resolved_at": null,
    36  			"resolved_by": null,
    37  			"secret_type": "mailchimp_api_key",
    38  			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
    39  		}]`)
    40  	})
    41  
    42  	ctx := context.Background()
    43  	opts := &SecretScanningAlertListOptions{State: "open", SecretType: "mailchimp_api_key"}
    44  
    45  	alerts, _, err := client.SecretScanning.ListAlertsForEnterprise(ctx, "e", opts)
    46  	if err != nil {
    47  		t.Errorf("SecretScanning.ListAlertsForEnterprise returned error: %v", err)
    48  	}
    49  
    50  	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
    51  	want := []*SecretScanningAlert{
    52  		{
    53  			Number:       Int(1),
    54  			CreatedAt:    &date,
    55  			URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
    56  			HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
    57  			LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
    58  			State:        String("open"),
    59  			Resolution:   nil,
    60  			ResolvedAt:   nil,
    61  			ResolvedBy:   nil,
    62  			SecretType:   String("mailchimp_api_key"),
    63  			Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
    64  		},
    65  	}
    66  
    67  	if !cmp.Equal(alerts, want) {
    68  		t.Errorf("SecretScanning.ListAlertsForEnterprise returned %+v, want %+v", alerts, want)
    69  	}
    70  
    71  	const methodName = "ListAlertsForEnterprise"
    72  
    73  	testBadOptions(t, methodName, func() (err error) {
    74  		_, _, err = client.SecretScanning.ListAlertsForEnterprise(ctx, "\n", opts)
    75  		return err
    76  	})
    77  
    78  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    79  		_, resp, err := client.SecretScanning.ListAlertsForEnterprise(ctx, "e", opts)
    80  		return resp, err
    81  	})
    82  }
    83  
    84  func TestSecretScanningService_ListAlertsForOrg(t *testing.T) {
    85  	client, mux, _, teardown := setup()
    86  	defer teardown()
    87  
    88  	mux.HandleFunc("/orgs/o/secret-scanning/alerts", func(w http.ResponseWriter, r *http.Request) {
    89  		testMethod(t, r, "GET")
    90  		testFormValues(t, r, values{"state": "open", "secret_type": "mailchimp_api_key"})
    91  
    92  		fmt.Fprint(w, `[{
    93  			"number": 1,
    94  			"created_at": "1996-06-20T00:00:00Z",
    95  			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
    96  			"html_url": "https://github.com/o/r/security/secret-scanning/1",
    97  			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
    98  			"state": "open",
    99  			"resolution": null,
   100  			"resolved_at": null,
   101  			"resolved_by": null,
   102  			"secret_type": "mailchimp_api_key",
   103  			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
   104  		}]`)
   105  	})
   106  
   107  	ctx := context.Background()
   108  	opts := &SecretScanningAlertListOptions{State: "open", SecretType: "mailchimp_api_key"}
   109  
   110  	alerts, _, err := client.SecretScanning.ListAlertsForOrg(ctx, "o", opts)
   111  	if err != nil {
   112  		t.Errorf("SecretScanning.ListAlertsForOrg returned error: %v", err)
   113  	}
   114  
   115  	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
   116  	want := []*SecretScanningAlert{
   117  		{
   118  			Number:       Int(1),
   119  			CreatedAt:    &date,
   120  			URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
   121  			HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
   122  			LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
   123  			State:        String("open"),
   124  			Resolution:   nil,
   125  			ResolvedAt:   nil,
   126  			ResolvedBy:   nil,
   127  			SecretType:   String("mailchimp_api_key"),
   128  			Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
   129  		},
   130  	}
   131  
   132  	if !cmp.Equal(alerts, want) {
   133  		t.Errorf("SecretScanning.ListAlertsForOrg returned %+v, want %+v", alerts, want)
   134  	}
   135  
   136  	const methodName = "ListAlertsForOrg"
   137  
   138  	testBadOptions(t, methodName, func() (err error) {
   139  		_, _, err = client.SecretScanning.ListAlertsForOrg(ctx, "\n", opts)
   140  		return err
   141  	})
   142  
   143  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   144  		_, resp, err := client.SecretScanning.ListAlertsForOrg(ctx, "o", opts)
   145  		return resp, err
   146  	})
   147  }
   148  
   149  func TestSecretScanningService_ListAlertsForRepo(t *testing.T) {
   150  	client, mux, _, teardown := setup()
   151  	defer teardown()
   152  
   153  	mux.HandleFunc("/repos/o/r/secret-scanning/alerts", func(w http.ResponseWriter, r *http.Request) {
   154  		testMethod(t, r, "GET")
   155  		testFormValues(t, r, values{"state": "open", "secret_type": "mailchimp_api_key"})
   156  
   157  		fmt.Fprint(w, `[{
   158  			"number": 1,
   159  			"created_at": "1996-06-20T00:00:00Z",
   160  			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
   161  			"html_url": "https://github.com/o/r/security/secret-scanning/1",
   162  			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
   163  			"state": "open",
   164  			"resolution": null,
   165  			"resolved_at": null,
   166  			"resolved_by": null,
   167  			"secret_type": "mailchimp_api_key",
   168  			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
   169  		}]`)
   170  	})
   171  
   172  	ctx := context.Background()
   173  	opts := &SecretScanningAlertListOptions{State: "open", SecretType: "mailchimp_api_key"}
   174  
   175  	alerts, _, err := client.SecretScanning.ListAlertsForRepo(ctx, "o", "r", opts)
   176  	if err != nil {
   177  		t.Errorf("SecretScanning.ListAlertsForRepo returned error: %v", err)
   178  	}
   179  
   180  	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
   181  	want := []*SecretScanningAlert{
   182  		{
   183  			Number:       Int(1),
   184  			CreatedAt:    &date,
   185  			URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
   186  			HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
   187  			LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
   188  			State:        String("open"),
   189  			Resolution:   nil,
   190  			ResolvedAt:   nil,
   191  			ResolvedBy:   nil,
   192  			SecretType:   String("mailchimp_api_key"),
   193  			Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
   194  		},
   195  	}
   196  
   197  	if !cmp.Equal(alerts, want) {
   198  		t.Errorf("SecretScanning.ListAlertsForRepo returned %+v, want %+v", alerts, want)
   199  	}
   200  
   201  	const methodName = "ListAlertsForRepo"
   202  
   203  	testBadOptions(t, methodName, func() (err error) {
   204  		_, _, err = client.SecretScanning.ListAlertsForRepo(ctx, "\n", "\n", opts)
   205  		return err
   206  	})
   207  
   208  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   209  		_, resp, err := client.SecretScanning.ListAlertsForRepo(ctx, "o", "r", opts)
   210  		return resp, err
   211  	})
   212  }
   213  
   214  func TestSecretScanningService_GetAlert(t *testing.T) {
   215  	client, mux, _, teardown := setup()
   216  	defer teardown()
   217  
   218  	mux.HandleFunc("/repos/o/r/secret-scanning/alerts/1", func(w http.ResponseWriter, r *http.Request) {
   219  		testMethod(t, r, "GET")
   220  
   221  		fmt.Fprint(w, `{
   222  			"number": 1,
   223  			"created_at": "1996-06-20T00:00:00Z",
   224  			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
   225  			"html_url": "https://github.com/o/r/security/secret-scanning/1",
   226  			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
   227  			"state": "open",
   228  			"resolution": null,
   229  			"resolved_at": null,
   230  			"resolved_by": null,
   231  			"secret_type": "mailchimp_api_key",
   232  			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
   233  		}`)
   234  	})
   235  
   236  	ctx := context.Background()
   237  
   238  	alert, _, err := client.SecretScanning.GetAlert(ctx, "o", "r", 1)
   239  	if err != nil {
   240  		t.Errorf("SecretScanning.GetAlert returned error: %v", err)
   241  	}
   242  
   243  	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
   244  	want := &SecretScanningAlert{
   245  		Number:       Int(1),
   246  		CreatedAt:    &date,
   247  		URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
   248  		HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
   249  		LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
   250  		State:        String("open"),
   251  		Resolution:   nil,
   252  		ResolvedAt:   nil,
   253  		ResolvedBy:   nil,
   254  		SecretType:   String("mailchimp_api_key"),
   255  		Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
   256  	}
   257  
   258  	if !cmp.Equal(alert, want) {
   259  		t.Errorf("SecretScanning.GetAlert returned %+v, want %+v", alert, want)
   260  	}
   261  
   262  	const methodName = "GetAlert"
   263  
   264  	testBadOptions(t, methodName, func() (err error) {
   265  		_, _, err = client.SecretScanning.GetAlert(ctx, "\n", "\n", 0)
   266  		return err
   267  	})
   268  
   269  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   270  		_, resp, err := client.SecretScanning.GetAlert(ctx, "o", "r", 1)
   271  		return resp, err
   272  	})
   273  }
   274  
   275  func TestSecretScanningService_UpdateAlert(t *testing.T) {
   276  	client, mux, _, teardown := setup()
   277  	defer teardown()
   278  
   279  	mux.HandleFunc("/repos/o/r/secret-scanning/alerts/1", func(w http.ResponseWriter, r *http.Request) {
   280  		testMethod(t, r, "PATCH")
   281  
   282  		v := new(SecretScanningAlertUpdateOptions)
   283  		json.NewDecoder(r.Body).Decode(v)
   284  
   285  		want := &SecretScanningAlertUpdateOptions{State: String("resolved"), Resolution: String("used_in_tests")}
   286  
   287  		if !cmp.Equal(v, want) {
   288  			t.Errorf("Request body = %+v, want %+v", v, want)
   289  		}
   290  
   291  		fmt.Fprint(w, `{
   292  			"number": 1,
   293  			"created_at": "1996-06-20T00:00:00Z",
   294  			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
   295  			"html_url": "https://github.com/o/r/security/secret-scanning/1",
   296  			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
   297  			"state": "resolved",
   298  			"resolution": "used_in_tests",
   299  			"resolved_at": "1996-06-20T00:00:00Z",
   300  			"resolved_by": null,
   301  			"secret_type": "mailchimp_api_key",
   302  			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
   303  		}`)
   304  	})
   305  
   306  	ctx := context.Background()
   307  	opts := &SecretScanningAlertUpdateOptions{State: String("resolved"), Resolution: String("used_in_tests")}
   308  
   309  	alert, _, err := client.SecretScanning.UpdateAlert(ctx, "o", "r", 1, opts)
   310  	if err != nil {
   311  		t.Errorf("SecretScanning.UpdateAlert returned error: %v", err)
   312  	}
   313  
   314  	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
   315  	want := &SecretScanningAlert{
   316  		Number:       Int(1),
   317  		CreatedAt:    &date,
   318  		URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
   319  		HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
   320  		LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
   321  		State:        String("resolved"),
   322  		Resolution:   String("used_in_tests"),
   323  		ResolvedAt:   &date,
   324  		ResolvedBy:   nil,
   325  		SecretType:   String("mailchimp_api_key"),
   326  		Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
   327  	}
   328  
   329  	if !cmp.Equal(alert, want) {
   330  		t.Errorf("SecretScanning.UpdateAlert returned %+v, want %+v", alert, want)
   331  	}
   332  
   333  	const methodName = "UpdateAlert"
   334  
   335  	testBadOptions(t, methodName, func() (err error) {
   336  		_, _, err = client.SecretScanning.UpdateAlert(ctx, "\n", "\n", 1, opts)
   337  		return err
   338  	})
   339  
   340  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   341  		_, resp, err := client.SecretScanning.UpdateAlert(ctx, "o", "r", 1, opts)
   342  		return resp, err
   343  	})
   344  }
   345  
   346  func TestSecretScanningService_ListLocationsForAlert(t *testing.T) {
   347  	client, mux, _, teardown := setup()
   348  	defer teardown()
   349  
   350  	mux.HandleFunc("/repos/o/r/secret-scanning/alerts/1/locations", func(w http.ResponseWriter, r *http.Request) {
   351  		testMethod(t, r, "GET")
   352  		testFormValues(t, r, values{"page": "1", "per_page": "100"})
   353  
   354  		fmt.Fprint(w, `[{
   355  			"type": "commit",
   356  			"details": {
   357  			  "path": "/example/secrets.txt",
   358  			  "start_line": 1,
   359  			  "end_line": 1,
   360  			  "start_column": 1,
   361  			  "end_column": 64,
   362  			  "blob_sha": "af5626b4a114abcb82d63db7c8082c3c4756e51b",
   363  			  "blob_url": "https://api.github.com/repos/o/r/git/blobs/af5626b4a114abcb82d63db7c8082c3c4756e51b",
   364  			  "commit_sha": "f14d7debf9775f957cf4f1e8176da0786431f72b",
   365  			  "commit_url": "https://api.github.com/repos/o/r/git/commits/f14d7debf9775f957cf4f1e8176da0786431f72b"
   366  			}
   367  		}]`)
   368  	})
   369  
   370  	ctx := context.Background()
   371  	opts := &ListOptions{Page: 1, PerPage: 100}
   372  
   373  	locations, _, err := client.SecretScanning.ListLocationsForAlert(ctx, "o", "r", 1, opts)
   374  	if err != nil {
   375  		t.Errorf("SecretScanning.ListLocationsForAlert returned error: %v", err)
   376  	}
   377  
   378  	want := []*SecretScanningAlertLocation{
   379  		{
   380  			Type: String("commit"),
   381  			Details: &SecretScanningAlertLocationDetails{
   382  				Path:        String("/example/secrets.txt"),
   383  				Startline:   Int(1),
   384  				EndLine:     Int(1),
   385  				StartColumn: Int(1),
   386  				EndColumn:   Int(64),
   387  				BlobSHA:     String("af5626b4a114abcb82d63db7c8082c3c4756e51b"),
   388  				BlobURL:     String("https://api.github.com/repos/o/r/git/blobs/af5626b4a114abcb82d63db7c8082c3c4756e51b"),
   389  				CommitSHA:   String("f14d7debf9775f957cf4f1e8176da0786431f72b"),
   390  				CommitURL:   String("https://api.github.com/repos/o/r/git/commits/f14d7debf9775f957cf4f1e8176da0786431f72b"),
   391  			},
   392  		},
   393  	}
   394  
   395  	if !cmp.Equal(locations, want) {
   396  		t.Errorf("SecretScanning.ListLocationsForAlert returned %+v, want %+v", locations, want)
   397  	}
   398  
   399  	const methodName = "ListLocationsForAlert"
   400  
   401  	testBadOptions(t, methodName, func() (err error) {
   402  		_, _, err = client.SecretScanning.ListLocationsForAlert(ctx, "\n", "\n", 1, opts)
   403  		return err
   404  	})
   405  
   406  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   407  		_, resp, err := client.SecretScanning.ListLocationsForAlert(ctx, "o", "r", 1, opts)
   408  		return resp, err
   409  	})
   410  }
   411  

View as plain text