github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/github/types_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestIssueCapsLogin(t *testing.T) {
    24  	// some valid logins that should all normalize to match the first
    25  	validLoginVariants := []string{
    26  		"BenTheElder",
    27  		"BENTHEELDER",
    28  		"bentheelder",
    29  		"BenTHEElder",
    30  		"BeNtHeElDeR",
    31  		"bEnThEeLdEr",
    32  	}
    33  	// add an explicitly normalized version for sanity
    34  	validLoginVariants = append(validLoginVariants, NormLogin(validLoginVariants[0]))
    35  
    36  	issue := Issue{
    37  		User: User{
    38  			Login: validLoginVariants[0],
    39  		},
    40  		Assignees: []User{
    41  			{
    42  				Login: validLoginVariants[0],
    43  			},
    44  		},
    45  	}
    46  	for _, login := range validLoginVariants {
    47  		if !issue.IsAuthor(login) {
    48  			t.Errorf("expected issue.IsAuthor(%s) to be true", login)
    49  		}
    50  		if !issue.IsAssignee(login) {
    51  			t.Errorf("expected issue.IsAssignee(%s) to be true", login)
    52  		}
    53  	}
    54  }
    55  
    56  func TestUnmarshalClientError(t *testing.T) {
    57  	var testcases = []struct {
    58  		name string
    59  		body string
    60  	}{
    61  		{
    62  			name: "invalid JSON",
    63  			body: `{"message":"Problems parsing JSON"}`,
    64  		},
    65  		{
    66  			name: "wrong type of JSON values",
    67  			body: `{"message":"Body should be a JSON object"}`,
    68  		},
    69  		{
    70  			name: "invalid fields",
    71  			body: `{
    72  				"message": "Validation Failed",
    73  				"errors": [
    74  				  {
    75  					"resource": "Issue",
    76  					"field": "title",
    77  					"code": "missing_field"
    78  				  }
    79  				]
    80  			  }`,
    81  		},
    82  		{
    83  			name: "requires authentication",
    84  			body: `{
    85  				"message": "Requires authentication",
    86  				"documentation_url": "https://developer.github.com/v3"
    87  			  }`,
    88  		},
    89  		{
    90  			name: "validation failed, position is invalid",
    91  			body: `{
    92  				"message": "Validation Failed",
    93  				"errors": [
    94  				  "Position is invalid"
    95  				],
    96  				"documentation_url": "https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review"
    97  			  }`,
    98  		},
    99  	}
   100  	for _, tc := range testcases {
   101  		b := []byte(tc.body)
   102  		err := unmarshalClientError(b)
   103  		_, isClientError := err.(ClientError)
   104  		_, isAlternativeClientError := err.(AlternativeClientError)
   105  		if !(isClientError || isAlternativeClientError) {
   106  			t.Errorf("For case %s, json.Unmarshal error: %v", tc.name, err)
   107  		}
   108  	}
   109  }