-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
types_test.go
83 lines (79 loc) · 2.54 KB
/
types_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jira
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
)
func TestErrorResultUnmarshal(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expectedOutput ErrorResult
assertErr require.ErrorAssertionFunc
}{
{
name: "new error",
input: `{"errorMessages":[], "errors": {"project": "project is required"}}`,
expectedOutput: ErrorResult{
Details: ErrorDetails{Errors: map[string]string{"project": "project is required"}},
},
assertErr: require.NoError,
},
{
name: "legacy error",
input: `{"errorMessages":["foo"],"errors":["bar", "baz"]}`,
expectedOutput: ErrorResult{
ErrorMessages: []string{"foo"},
Details: ErrorDetails{LegacyErrors: []string{"bar", "baz"}},
},
assertErr: require.NoError,
},
{
name: "empty error",
input: `{"errorMessages":["There was an error parsing JSON. Check that your request body is valid."]}`,
expectedOutput: ErrorResult{
ErrorMessages: []string{"There was an error parsing JSON. Check that your request body is valid."},
},
assertErr: require.NoError,
},
{
name: "malformed error",
input: `{"errorMessages":["Foo"],"errors":"This is a single string"}`,
expectedOutput: ErrorResult{ErrorMessages: []string{"Foo"}},
assertErr: func(t require.TestingT, err error, i ...interface{}) {
require.ErrorContains(t, err, "This is a single string")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt := tt
t.Parallel()
data := []byte(tt.input)
var result ErrorResult
tt.assertErr(t, json.Unmarshal(data, &result))
diff := cmp.Diff(tt.expectedOutput, result, cmpopts.EquateEmpty())
require.Empty(t, diff)
})
}
}