github.com/secman-team/gh-api@v1.8.2/api/queries_pr_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/MakeNowJust/heredoc"
     8  	"github.com/secman-team/gh-api/core/ghrepo"
     9  	"github.com/secman-team/gh-api/pkg/httpmock"
    10  )
    11  
    12  func TestBranchDeleteRemote(t *testing.T) {
    13  	var tests = []struct {
    14  		name           string
    15  		responseStatus int
    16  		responseBody   string
    17  		expectError    bool
    18  	}{
    19  		{
    20  			name:           "success",
    21  			responseStatus: 204,
    22  			responseBody:   "",
    23  			expectError:    false,
    24  		},
    25  		{
    26  			name:           "error",
    27  			responseStatus: 500,
    28  			responseBody:   `{"message": "oh no"}`,
    29  			expectError:    true,
    30  		},
    31  	}
    32  
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			http := &httpmock.Registry{}
    36  			http.Register(
    37  				httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/branch"),
    38  				httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody))
    39  
    40  			client := NewClient(ReplaceTripper(http))
    41  			repo, _ := ghrepo.FromFullName("OWNER/REPO")
    42  
    43  			err := BranchDeleteRemote(client, repo, "branch")
    44  			if (err != nil) != tt.expectError {
    45  				t.Fatalf("unexpected result: %v", err)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func Test_determinePullRequestFeatures(t *testing.T) {
    52  	tests := []struct {
    53  		name           string
    54  		hostname       string
    55  		queryResponse  map[string]string
    56  		wantPrFeatures pullRequestFeature
    57  		wantErr        bool
    58  	}{
    59  		{
    60  			name:     "github.com",
    61  			hostname: "github.com",
    62  			wantPrFeatures: pullRequestFeature{
    63  				HasReviewDecision:       true,
    64  				HasStatusCheckRollup:    true,
    65  				HasBranchProtectionRule: true,
    66  			},
    67  			wantErr: false,
    68  		},
    69  		{
    70  			name:     "GHE empty response",
    71  			hostname: "git.my.org",
    72  			queryResponse: map[string]string{
    73  				`query PullRequest_fields\b`:  `{"data": {}}`,
    74  				`query PullRequest_fields2\b`: `{"data": {}}`,
    75  			},
    76  			wantPrFeatures: pullRequestFeature{
    77  				HasReviewDecision:       false,
    78  				HasStatusCheckRollup:    false,
    79  				HasBranchProtectionRule: false,
    80  			},
    81  			wantErr: false,
    82  		},
    83  		{
    84  			name:     "GHE has reviewDecision",
    85  			hostname: "git.my.org",
    86  			queryResponse: map[string]string{
    87  				`query PullRequest_fields\b`: heredoc.Doc(`
    88  					{ "data": { "PullRequest": { "fields": [
    89  						{"name": "foo"},
    90  						{"name": "reviewDecision"}
    91  					] } } }
    92  				`),
    93  				`query PullRequest_fields2\b`: `{"data": {}}`,
    94  			},
    95  			wantPrFeatures: pullRequestFeature{
    96  				HasReviewDecision:       true,
    97  				HasStatusCheckRollup:    false,
    98  				HasBranchProtectionRule: false,
    99  			},
   100  			wantErr: false,
   101  		},
   102  		{
   103  			name:     "GHE has statusCheckRollup",
   104  			hostname: "git.my.org",
   105  			queryResponse: map[string]string{
   106  				`query PullRequest_fields\b`: heredoc.Doc(`
   107  					{ "data": { "Commit": { "fields": [
   108  						{"name": "foo"},
   109  						{"name": "statusCheckRollup"}
   110  					] } } }
   111  				`),
   112  				`query PullRequest_fields2\b`: `{"data": {}}`,
   113  			},
   114  			wantPrFeatures: pullRequestFeature{
   115  				HasReviewDecision:       false,
   116  				HasStatusCheckRollup:    true,
   117  				HasBranchProtectionRule: false,
   118  			},
   119  			wantErr: false,
   120  		},
   121  		{
   122  			name:     "GHE has branchProtectionRule",
   123  			hostname: "git.my.org",
   124  			queryResponse: map[string]string{
   125  				`query PullRequest_fields\b`: `{"data": {}}`,
   126  				`query PullRequest_fields2\b`: heredoc.Doc(`
   127  					{ "data": { "Ref": { "fields": [
   128  						{"name": "foo"},
   129  						{"name": "branchProtectionRule"}
   130  					] } } }
   131  				`),
   132  			},
   133  			wantPrFeatures: pullRequestFeature{
   134  				HasReviewDecision:       false,
   135  				HasStatusCheckRollup:    false,
   136  				HasBranchProtectionRule: true,
   137  			},
   138  			wantErr: false,
   139  		},
   140  	}
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			fakeHTTP := &httpmock.Registry{}
   144  			httpClient := NewHTTPClient(ReplaceTripper(fakeHTTP))
   145  
   146  			for query, resp := range tt.queryResponse {
   147  				fakeHTTP.Register(httpmock.GraphQL(query), httpmock.StringResponse(resp))
   148  			}
   149  
   150  			gotPrFeatures, err := determinePullRequestFeatures(httpClient, tt.hostname)
   151  			if (err != nil) != tt.wantErr {
   152  				t.Errorf("determinePullRequestFeatures() error = %v, wantErr %v", err, tt.wantErr)
   153  				return
   154  			}
   155  			if !reflect.DeepEqual(gotPrFeatures, tt.wantPrFeatures) {
   156  				t.Errorf("determinePullRequestFeatures() = %v, want %v", gotPrFeatures, tt.wantPrFeatures)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func Test_sortPullRequestsByState(t *testing.T) {
   163  	prs := []PullRequest{
   164  		{
   165  			BaseRefName: "test1",
   166  			State:       "MERGED",
   167  		},
   168  		{
   169  			BaseRefName: "test2",
   170  			State:       "CLOSED",
   171  		},
   172  		{
   173  			BaseRefName: "test3",
   174  			State:       "OPEN",
   175  		},
   176  	}
   177  
   178  	sortPullRequestsByState(prs)
   179  
   180  	if prs[0].BaseRefName != "test3" {
   181  		t.Errorf("prs[0]: got %s, want %q", prs[0].BaseRefName, "test3")
   182  	}
   183  	if prs[1].BaseRefName != "test1" {
   184  		t.Errorf("prs[1]: got %s, want %q", prs[1].BaseRefName, "test1")
   185  	}
   186  	if prs[2].BaseRefName != "test2" {
   187  		t.Errorf("prs[2]: got %s, want %q", prs[2].BaseRefName, "test2")
   188  	}
   189  }