1 package format
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7
8 "edge-infra.dev/pkg/edge/api/graph/model"
9 )
10
11 func TestGenerateOutput(t *testing.T) {
12 t.Parallel()
13
14 invalidRole := "EDGE_INVALID_ROLE"
15 emptyRole := ""
16 invalidPriv := "ea-invalid-priv"
17
18 tests := map[string]struct {
19 function func(inputType string, errors []*model.OperatorInterventionErrorResponse) string
20 inputType string
21 e []*model.OperatorInterventionErrorResponse
22 exp string
23 }{
24 "Delete No Errors": {
25 function: GenerateDeleteOutput,
26 inputType: "Role Mappings",
27 e: nil,
28 exp: "\nOI Role Mappings deleted\n",
29 },
30 "Delete One Error": {
31 function: GenerateDeleteOutput,
32 inputType: "Role Mappings",
33 e: []*model.OperatorInterventionErrorResponse{
34 {
35 Type: model.OperatorInterventionErrorTypeUnknownRole,
36 Role: &invalidRole,
37 },
38 },
39 exp: "\nErrors occurred when deleting OI Role Mappings: \n" +
40 "\tError: UNKNOWN_ROLE. Details: Role: \"EDGE_INVALID_ROLE\"\n" +
41 "\nOI Role Mappings not deleted.\n",
42 },
43 "Delete Two Errors": {
44 function: GenerateDeleteOutput,
45 inputType: "Rule",
46 e: []*model.OperatorInterventionErrorResponse{
47 {
48 Type: model.OperatorInterventionErrorTypeUnknownRole,
49 Role: &invalidRole,
50 },
51 {
52 Type: model.OperatorInterventionErrorTypeUnknownPrivilege,
53 Privilege: &invalidPriv,
54 },
55 },
56 exp: "\nErrors occurred when deleting OI Rule: \n" +
57 "\tError: UNKNOWN_ROLE. Details: Role: \"EDGE_INVALID_ROLE\"\n" +
58 "\tError: UNKNOWN_PRIVILEGE. Details: Privilege: \"ea-invalid-priv\"\n" +
59 "\nOI Rule not deleted.\n",
60 },
61 "Delete Empty Role": {
62 function: GenerateDeleteOutput,
63 inputType: "Role Mappings",
64 e: []*model.OperatorInterventionErrorResponse{
65 {
66 Type: model.OperatorInterventionErrorTypeUnknownRole,
67 Role: &emptyRole,
68 },
69 },
70 exp: "\nErrors occurred when deleting OI Role Mappings: \n" +
71 "\tError: UNKNOWN_ROLE. Details: Role: \"\"\n" +
72 "\nOI Role Mappings not deleted.\n",
73 },
74 "Apply No Errors": {
75 function: GenerateApplyOutput,
76 inputType: "Role Mappings",
77 e: nil,
78 exp: "\nOI Role Mappings applied\n",
79 },
80 "Apply One Error": {
81 function: GenerateApplyOutput,
82 inputType: "Role Mappings",
83 e: []*model.OperatorInterventionErrorResponse{
84 {
85 Type: model.OperatorInterventionErrorTypeUnknownRole,
86 Role: &invalidRole,
87 },
88 },
89 exp: "\nErrors occurred when applying OI Role Mappings: \n" +
90 "\tError: UNKNOWN_ROLE. Details: Role: \"EDGE_INVALID_ROLE\"\n" +
91 "\nOI Role Mappings not applied.\n",
92 },
93 "Apply Two Errors": {
94 function: GenerateApplyOutput,
95 inputType: "Rule",
96 e: []*model.OperatorInterventionErrorResponse{
97 {
98 Type: model.OperatorInterventionErrorTypeUnknownRole,
99 Role: &invalidRole,
100 },
101 {
102 Type: model.OperatorInterventionErrorTypeUnknownPrivilege,
103 Privilege: &invalidPriv,
104 },
105 },
106 exp: "\nErrors occurred when applying OI Rule: \n" +
107 "\tError: UNKNOWN_ROLE. Details: Role: \"EDGE_INVALID_ROLE\"\n" +
108 "\tError: UNKNOWN_PRIVILEGE. Details: Privilege: \"ea-invalid-priv\"\n" +
109 "\nOI Rule not applied.\n",
110 },
111 }
112
113 for name, tc := range tests {
114 tc := tc
115 t.Run(name, func(t *testing.T) {
116 t.Parallel()
117
118 out := tc.function(tc.inputType, tc.e)
119
120 require.Equal(t, tc.exp, out)
121 })
122 }
123 }
124
View as plain text