github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/systemauth/converter_test.go (about) 1 package systemauth_test 2 3 import ( 4 "testing" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/model" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/systemauth/automock" 9 10 "github.com/kyma-incubator/compass/components/director/internal/domain/systemauth" 11 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestConverter_ToGraphQL(t *testing.T) { 18 // GIVEN 19 sysAuthID := "foo" 20 objectID := "bar" 21 22 modelAuth := fixModelAuth() 23 gqlAuth := fixGQLAuth() 24 modelRtmSysAuth := fixModelSystemAuth(sysAuthID, model.RuntimeReference, objectID, modelAuth) 25 modelAppSysAuth := fixModelSystemAuth(sysAuthID, model.ApplicationReference, objectID, modelAuth) 26 modelIntSysAuth := fixModelSystemAuth(sysAuthID, model.IntegrationSystemReference, objectID, modelAuth) 27 28 testCases := []struct { 29 Name string 30 AuthConvFn func() *automock.AuthConverter 31 Input *model.SystemAuth 32 ExpectedOutput graphql.SystemAuth 33 }{ 34 { 35 Name: "Success when converting auth for Runtime", 36 AuthConvFn: func() *automock.AuthConverter { 37 authConv := &automock.AuthConverter{} 38 authConv.On("ToGraphQL", modelAuth).Return(gqlAuth, nil).Once() 39 return authConv 40 }, 41 Input: modelRtmSysAuth, 42 ExpectedOutput: fixGQLRuntimeSystemAuth(sysAuthID, gqlAuth, objectID), 43 }, 44 { 45 Name: "Success when converting auth for Application", 46 AuthConvFn: func() *automock.AuthConverter { 47 authConv := &automock.AuthConverter{} 48 authConv.On("ToGraphQL", modelAuth).Return(gqlAuth, nil).Once() 49 return authConv 50 }, 51 Input: modelAppSysAuth, 52 ExpectedOutput: fixGQLAppSystemAuth(sysAuthID, gqlAuth, objectID), 53 }, 54 { 55 Name: "Success when converting auth for Integration System", 56 AuthConvFn: func() *automock.AuthConverter { 57 authConv := &automock.AuthConverter{} 58 authConv.On("ToGraphQL", modelAuth).Return(gqlAuth, nil).Once() 59 return authConv 60 }, 61 Input: modelIntSysAuth, 62 ExpectedOutput: fixGQLIntSysSystemAuth(sysAuthID, gqlAuth, objectID), 63 }, 64 { 65 Name: "Returns nil when input is nil", 66 AuthConvFn: func() *automock.AuthConverter { 67 authConv := &automock.AuthConverter{} 68 return authConv 69 }, 70 Input: nil, 71 ExpectedOutput: nil, 72 }, 73 } 74 75 for _, testCase := range testCases { 76 t.Run(testCase.Name, func(t *testing.T) { 77 authConv := testCase.AuthConvFn() 78 conv := systemauth.NewConverter(authConv) 79 80 // WHEN 81 result, err := conv.ToGraphQL(testCase.Input) 82 83 // THEN 84 assert.NoError(t, err) 85 assert.Equal(t, testCase.ExpectedOutput, result) 86 authConv.AssertExpectations(t) 87 }) 88 } 89 } 90 91 func TestConverter_ToEntity(t *testing.T) { 92 // GIVEN 93 sysAuthID := "foo" 94 objectID := "bar" 95 96 modelAuth := fixModelAuth() 97 modelRtmSysAuth := *fixModelSystemAuth(sysAuthID, model.RuntimeReference, objectID, modelAuth) 98 modelAppSysAuth := *fixModelSystemAuth(sysAuthID, model.ApplicationReference, objectID, modelAuth) 99 modelIntSysAuth := *fixModelSystemAuth(sysAuthID, model.IntegrationSystemReference, objectID, modelAuth) 100 101 entRtm := fixEntity(sysAuthID, model.RuntimeReference, objectID, true) 102 entApp := fixEntity(sysAuthID, model.ApplicationReference, objectID, true) 103 entInt := fixEntity(sysAuthID, model.IntegrationSystemReference, objectID, true) 104 105 testCases := []struct { 106 Name string 107 Input model.SystemAuth 108 ExpectedOutput systemauth.Entity 109 ExpectedError error 110 }{ 111 { 112 Name: "Success when converting auth for Runtime", 113 Input: modelRtmSysAuth, 114 ExpectedOutput: entRtm, 115 ExpectedError: nil, 116 }, 117 { 118 Name: "Success when converting auth for Application", 119 Input: modelAppSysAuth, 120 ExpectedOutput: entApp, 121 ExpectedError: nil, 122 }, 123 { 124 Name: "Success when converting auth for Integration System", 125 Input: modelIntSysAuth, 126 ExpectedOutput: entInt, 127 ExpectedError: nil, 128 }, 129 } 130 131 for _, testCase := range testCases { 132 t.Run(testCase.Name, func(t *testing.T) { 133 conv := systemauth.NewConverter(nil) 134 135 // WHEN 136 result, err := conv.ToEntity(testCase.Input) 137 138 // THEN 139 if testCase.ExpectedError != nil { 140 require.Error(t, err) 141 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 142 } else { 143 assert.NoError(t, err) 144 } 145 assert.Equal(t, testCase.ExpectedOutput, result) 146 }) 147 } 148 } 149 150 func TestConverter_FromEntity(t *testing.T) { 151 // GIVEN 152 sysAuthID := "foo" 153 objectID := "bar" 154 155 modelAuth := fixModelAuth() 156 modelRtmSysAuth := *fixModelSystemAuth(sysAuthID, model.RuntimeReference, objectID, modelAuth) 157 modelAppSysAuth := *fixModelSystemAuth(sysAuthID, model.ApplicationReference, objectID, modelAuth) 158 modelIntSysAuth := *fixModelSystemAuth(sysAuthID, model.IntegrationSystemReference, objectID, modelAuth) 159 160 entRtm := fixEntity(sysAuthID, model.RuntimeReference, objectID, true) 161 entApp := fixEntity(sysAuthID, model.ApplicationReference, objectID, true) 162 entInt := fixEntity(sysAuthID, model.IntegrationSystemReference, objectID, true) 163 164 testCases := []struct { 165 Name string 166 Input systemauth.Entity 167 ExpectedOutput model.SystemAuth 168 ExpectedError error 169 }{ 170 { 171 Name: "Success when converting auth for Runtime", 172 Input: entRtm, 173 ExpectedOutput: modelRtmSysAuth, 174 ExpectedError: nil, 175 }, 176 { 177 Name: "Success when converting auth for Application", 178 Input: entApp, 179 ExpectedOutput: modelAppSysAuth, 180 ExpectedError: nil, 181 }, 182 { 183 Name: "Success when converting auth for Integration System", 184 Input: entInt, 185 ExpectedOutput: modelIntSysAuth, 186 ExpectedError: nil, 187 }, 188 } 189 190 for _, testCase := range testCases { 191 t.Run(testCase.Name, func(t *testing.T) { 192 conv := systemauth.NewConverter(nil) 193 194 // WHEN 195 result, err := conv.FromEntity(testCase.Input) 196 197 // THEN 198 if testCase.ExpectedError != nil { 199 require.Error(t, err) 200 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 201 } else { 202 assert.NoError(t, err) 203 } 204 assert.Equal(t, testCase.ExpectedOutput, result) 205 }) 206 } 207 }