|
| 1 | +package userroles |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/supertokens/supertokens-golang/recipe/session" |
| 8 | + "github.com/supertokens/supertokens-golang/recipe/session/claims" |
| 9 | + sessErrors "github.com/supertokens/supertokens-golang/recipe/session/errors" |
| 10 | + "github.com/supertokens/supertokens-golang/recipe/userroles/userrolesclaims" |
| 11 | + "github.com/supertokens/supertokens-golang/recipe/userroles/userrolesmodels" |
| 12 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 13 | + "github.com/supertokens/supertokens-golang/test/unittesting" |
| 14 | +) |
| 15 | + |
| 16 | +func TestShouldAddClaimsToSessionWithoutConfig(t *testing.T) { |
| 17 | + configValue := supertokens.TypeInput{ |
| 18 | + Supertokens: &supertokens.ConnectionInfo{ |
| 19 | + ConnectionURI: "http://localhost:8080", |
| 20 | + }, |
| 21 | + AppInfo: supertokens.AppInfo{ |
| 22 | + AppName: "SuperTokens", |
| 23 | + WebsiteDomain: "supertokens.io", |
| 24 | + APIDomain: "api.supertokens.io", |
| 25 | + }, |
| 26 | + RecipeList: []supertokens.Recipe{ |
| 27 | + session.Init(nil), |
| 28 | + Init(nil), |
| 29 | + }, |
| 30 | + } |
| 31 | + BeforeEach() |
| 32 | + unittesting.StartUpST("localhost", "8080") |
| 33 | + defer AfterEach() |
| 34 | + err := supertokens.Init(configValue) |
| 35 | + if err != nil { |
| 36 | + t.Error(err.Error()) |
| 37 | + } |
| 38 | + |
| 39 | + if !canRunTest(t) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + res := fakeRes{} |
| 44 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 45 | + assert.NoError(t, err) |
| 46 | + |
| 47 | + userroleClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.UserRoleClaim) |
| 48 | + assert.NoError(t, err) |
| 49 | + |
| 50 | + assert.NotNil(t, userroleClaimValue.OK) |
| 51 | + assert.Equal(t, []interface{}{}, userroleClaimValue.OK.Value) |
| 52 | + |
| 53 | + permissionClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.PermissionClaim) |
| 54 | + assert.NoError(t, err) |
| 55 | + |
| 56 | + assert.NotNil(t, permissionClaimValue.OK) |
| 57 | + assert.Equal(t, []interface{}{}, permissionClaimValue.OK.Value) |
| 58 | +} |
| 59 | + |
| 60 | +func TestShouldNotAddClaimsToSessionIfDisabledInConfig(t *testing.T) { |
| 61 | + configValue := supertokens.TypeInput{ |
| 62 | + Supertokens: &supertokens.ConnectionInfo{ |
| 63 | + ConnectionURI: "http://localhost:8080", |
| 64 | + }, |
| 65 | + AppInfo: supertokens.AppInfo{ |
| 66 | + AppName: "SuperTokens", |
| 67 | + WebsiteDomain: "supertokens.io", |
| 68 | + APIDomain: "api.supertokens.io", |
| 69 | + }, |
| 70 | + RecipeList: []supertokens.Recipe{ |
| 71 | + session.Init(nil), |
| 72 | + Init(&userrolesmodels.TypeInput{ |
| 73 | + SkipAddingRolesToAccessToken: true, |
| 74 | + SkipAddingPermissionsToAccessToken: true, |
| 75 | + }), |
| 76 | + }, |
| 77 | + } |
| 78 | + BeforeEach() |
| 79 | + unittesting.StartUpST("localhost", "8080") |
| 80 | + defer AfterEach() |
| 81 | + err := supertokens.Init(configValue) |
| 82 | + if err != nil { |
| 83 | + t.Error(err.Error()) |
| 84 | + } |
| 85 | + |
| 86 | + if !canRunTest(t) { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + res := fakeRes{} |
| 91 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 92 | + assert.NoError(t, err) |
| 93 | + |
| 94 | + userroleClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.UserRoleClaim) |
| 95 | + assert.NoError(t, err) |
| 96 | + |
| 97 | + assert.NotNil(t, userroleClaimValue.OK) |
| 98 | + assert.Equal(t, nil, userroleClaimValue.OK.Value) |
| 99 | + |
| 100 | + permissionClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.PermissionClaim) |
| 101 | + assert.NoError(t, err) |
| 102 | + |
| 103 | + assert.NotNil(t, permissionClaimValue.OK) |
| 104 | + assert.Equal(t, nil, permissionClaimValue.OK.Value) |
| 105 | +} |
| 106 | + |
| 107 | +func TestShouldAddClaimsToSessionWithValues(t *testing.T) { |
| 108 | + configValue := supertokens.TypeInput{ |
| 109 | + Supertokens: &supertokens.ConnectionInfo{ |
| 110 | + ConnectionURI: "http://localhost:8080", |
| 111 | + }, |
| 112 | + AppInfo: supertokens.AppInfo{ |
| 113 | + AppName: "SuperTokens", |
| 114 | + WebsiteDomain: "supertokens.io", |
| 115 | + APIDomain: "api.supertokens.io", |
| 116 | + }, |
| 117 | + RecipeList: []supertokens.Recipe{ |
| 118 | + session.Init(nil), |
| 119 | + Init(nil), |
| 120 | + }, |
| 121 | + } |
| 122 | + BeforeEach() |
| 123 | + unittesting.StartUpST("localhost", "8080") |
| 124 | + defer AfterEach() |
| 125 | + err := supertokens.Init(configValue) |
| 126 | + if err != nil { |
| 127 | + t.Error(err.Error()) |
| 128 | + } |
| 129 | + |
| 130 | + if !canRunTest(t) { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + CreateNewRoleOrAddPermissions("test", []string{"a", "b"}, &map[string]interface{}{}) |
| 135 | + AddRoleToUser("userId", "test", &map[string]interface{}{}) |
| 136 | + |
| 137 | + res := fakeRes{} |
| 138 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 139 | + assert.NoError(t, err) |
| 140 | + |
| 141 | + userroleClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.UserRoleClaim) |
| 142 | + assert.NoError(t, err) |
| 143 | + |
| 144 | + assert.NotNil(t, userroleClaimValue.OK) |
| 145 | + assert.Equal(t, []interface{}{"test"}, userroleClaimValue.OK.Value) |
| 146 | + |
| 147 | + permissionClaimValue, err := session.GetClaimValue(sessionContainer.GetHandle(), userrolesclaims.PermissionClaim) |
| 148 | + assert.NoError(t, err) |
| 149 | + |
| 150 | + assert.NotNil(t, permissionClaimValue.OK) |
| 151 | + assert.Equal(t, 2, len(permissionClaimValue.OK.Value.([]interface{}))) |
| 152 | + assert.Equal(t, []interface{}{"a", "b"}, permissionClaimValue.OK.Value) |
| 153 | + assert.Contains(t, permissionClaimValue.OK.Value, "a") |
| 154 | + assert.Contains(t, permissionClaimValue.OK.Value, "b") |
| 155 | +} |
| 156 | + |
| 157 | +func TestShouldValidateRoles(t *testing.T) { |
| 158 | + configValue := supertokens.TypeInput{ |
| 159 | + Supertokens: &supertokens.ConnectionInfo{ |
| 160 | + ConnectionURI: "http://localhost:8080", |
| 161 | + }, |
| 162 | + AppInfo: supertokens.AppInfo{ |
| 163 | + AppName: "SuperTokens", |
| 164 | + WebsiteDomain: "supertokens.io", |
| 165 | + APIDomain: "api.supertokens.io", |
| 166 | + }, |
| 167 | + RecipeList: []supertokens.Recipe{ |
| 168 | + session.Init(nil), |
| 169 | + Init(nil), |
| 170 | + }, |
| 171 | + } |
| 172 | + BeforeEach() |
| 173 | + unittesting.StartUpST("localhost", "8080") |
| 174 | + defer AfterEach() |
| 175 | + err := supertokens.Init(configValue) |
| 176 | + if err != nil { |
| 177 | + t.Error(err.Error()) |
| 178 | + } |
| 179 | + |
| 180 | + if !canRunTest(t) { |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + CreateNewRoleOrAddPermissions("test", []string{"a", "b"}, &map[string]interface{}{}) |
| 185 | + AddRoleToUser("userId", "test", &map[string]interface{}{}) |
| 186 | + |
| 187 | + res := fakeRes{} |
| 188 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 189 | + assert.NoError(t, err) |
| 190 | + |
| 191 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 192 | + userrolesclaims.UserRoleClaimValidators.Includes("test", nil, nil), |
| 193 | + }) |
| 194 | + assert.NoError(t, err) |
| 195 | + |
| 196 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 197 | + userrolesclaims.UserRoleClaimValidators.Includes("nope", nil, nil), |
| 198 | + }) |
| 199 | + assert.NotNil(t, err) |
| 200 | + assert.IsType(t, sessErrors.InvalidClaimError{}, err) |
| 201 | + |
| 202 | + invalidClaimErr := err.(sessErrors.InvalidClaimError) |
| 203 | + assert.Equal(t, 1, len(invalidClaimErr.InvalidClaims)) |
| 204 | + assert.Equal(t, "st-role", invalidClaimErr.InvalidClaims[0].ID) |
| 205 | + assert.Equal(t, map[string]interface{}{ |
| 206 | + "actualValue": []interface{}{"test"}, |
| 207 | + "expectedToInclude": "nope", |
| 208 | + "message": "wrong value", |
| 209 | + }, invalidClaimErr.InvalidClaims[0].Reason) |
| 210 | +} |
| 211 | + |
| 212 | +func TestShouldValidateRolesAfterRefetching(t *testing.T) { |
| 213 | + configValue := supertokens.TypeInput{ |
| 214 | + Supertokens: &supertokens.ConnectionInfo{ |
| 215 | + ConnectionURI: "http://localhost:8080", |
| 216 | + }, |
| 217 | + AppInfo: supertokens.AppInfo{ |
| 218 | + AppName: "SuperTokens", |
| 219 | + WebsiteDomain: "supertokens.io", |
| 220 | + APIDomain: "api.supertokens.io", |
| 221 | + }, |
| 222 | + RecipeList: []supertokens.Recipe{ |
| 223 | + session.Init(nil), |
| 224 | + Init(&userrolesmodels.TypeInput{ |
| 225 | + SkipAddingRolesToAccessToken: true, |
| 226 | + }), |
| 227 | + }, |
| 228 | + } |
| 229 | + BeforeEach() |
| 230 | + unittesting.StartUpST("localhost", "8080") |
| 231 | + defer AfterEach() |
| 232 | + err := supertokens.Init(configValue) |
| 233 | + if err != nil { |
| 234 | + t.Error(err.Error()) |
| 235 | + } |
| 236 | + |
| 237 | + if !canRunTest(t) { |
| 238 | + return |
| 239 | + } |
| 240 | + |
| 241 | + CreateNewRoleOrAddPermissions("test", []string{"a", "b"}, &map[string]interface{}{}) |
| 242 | + AddRoleToUser("userId", "test", &map[string]interface{}{}) |
| 243 | + |
| 244 | + res := fakeRes{} |
| 245 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 246 | + assert.NoError(t, err) |
| 247 | + |
| 248 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 249 | + userrolesclaims.UserRoleClaimValidators.Includes("test", nil, nil), |
| 250 | + }) |
| 251 | + assert.NoError(t, err) |
| 252 | + |
| 253 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 254 | + userrolesclaims.UserRoleClaimValidators.Includes("nope", nil, nil), |
| 255 | + }) |
| 256 | + assert.NotNil(t, err) |
| 257 | + assert.IsType(t, sessErrors.InvalidClaimError{}, err) |
| 258 | + |
| 259 | + invalidClaimErr := err.(sessErrors.InvalidClaimError) |
| 260 | + assert.Equal(t, 1, len(invalidClaimErr.InvalidClaims)) |
| 261 | + assert.Equal(t, "st-role", invalidClaimErr.InvalidClaims[0].ID) |
| 262 | + assert.Equal(t, map[string]interface{}{ |
| 263 | + "actualValue": []interface{}{"test"}, |
| 264 | + "expectedToInclude": "nope", |
| 265 | + "message": "wrong value", |
| 266 | + }, invalidClaimErr.InvalidClaims[0].Reason) |
| 267 | +} |
| 268 | + |
| 269 | +func TestShouldValidatePermissions(t *testing.T) { |
| 270 | + configValue := supertokens.TypeInput{ |
| 271 | + Supertokens: &supertokens.ConnectionInfo{ |
| 272 | + ConnectionURI: "http://localhost:8080", |
| 273 | + }, |
| 274 | + AppInfo: supertokens.AppInfo{ |
| 275 | + AppName: "SuperTokens", |
| 276 | + WebsiteDomain: "supertokens.io", |
| 277 | + APIDomain: "api.supertokens.io", |
| 278 | + }, |
| 279 | + RecipeList: []supertokens.Recipe{ |
| 280 | + session.Init(nil), |
| 281 | + Init(nil), |
| 282 | + }, |
| 283 | + } |
| 284 | + BeforeEach() |
| 285 | + unittesting.StartUpST("localhost", "8080") |
| 286 | + defer AfterEach() |
| 287 | + err := supertokens.Init(configValue) |
| 288 | + if err != nil { |
| 289 | + t.Error(err.Error()) |
| 290 | + } |
| 291 | + |
| 292 | + if !canRunTest(t) { |
| 293 | + return |
| 294 | + } |
| 295 | + |
| 296 | + CreateNewRoleOrAddPermissions("test", []string{"a", "b"}, &map[string]interface{}{}) |
| 297 | + AddRoleToUser("userId", "test", &map[string]interface{}{}) |
| 298 | + |
| 299 | + res := fakeRes{} |
| 300 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 301 | + assert.NoError(t, err) |
| 302 | + |
| 303 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 304 | + userrolesclaims.PermissionClaimValidators.Includes("a", nil, nil), |
| 305 | + }) |
| 306 | + assert.NoError(t, err) |
| 307 | + |
| 308 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 309 | + userrolesclaims.PermissionClaimValidators.Includes("nope", nil, nil), |
| 310 | + }) |
| 311 | + assert.NotNil(t, err) |
| 312 | + assert.IsType(t, sessErrors.InvalidClaimError{}, err) |
| 313 | + |
| 314 | + invalidClaimErr := err.(sessErrors.InvalidClaimError) |
| 315 | + assert.Equal(t, 1, len(invalidClaimErr.InvalidClaims)) |
| 316 | + assert.Equal(t, "st-perm", invalidClaimErr.InvalidClaims[0].ID) |
| 317 | + assert.Equal(t, map[string]interface{}{ |
| 318 | + "actualValue": []interface{}{"a", "b"}, |
| 319 | + "expectedToInclude": "nope", |
| 320 | + "message": "wrong value", |
| 321 | + }, invalidClaimErr.InvalidClaims[0].Reason) |
| 322 | +} |
| 323 | + |
| 324 | +func TestShouldValidatePermissionsAfterRefetching(t *testing.T) { |
| 325 | + configValue := supertokens.TypeInput{ |
| 326 | + Supertokens: &supertokens.ConnectionInfo{ |
| 327 | + ConnectionURI: "http://localhost:8080", |
| 328 | + }, |
| 329 | + AppInfo: supertokens.AppInfo{ |
| 330 | + AppName: "SuperTokens", |
| 331 | + WebsiteDomain: "supertokens.io", |
| 332 | + APIDomain: "api.supertokens.io", |
| 333 | + }, |
| 334 | + RecipeList: []supertokens.Recipe{ |
| 335 | + session.Init(nil), |
| 336 | + Init(&userrolesmodels.TypeInput{ |
| 337 | + SkipAddingPermissionsToAccessToken: true, |
| 338 | + }), |
| 339 | + }, |
| 340 | + } |
| 341 | + BeforeEach() |
| 342 | + unittesting.StartUpST("localhost", "8080") |
| 343 | + defer AfterEach() |
| 344 | + err := supertokens.Init(configValue) |
| 345 | + if err != nil { |
| 346 | + t.Error(err.Error()) |
| 347 | + } |
| 348 | + |
| 349 | + if !canRunTest(t) { |
| 350 | + return |
| 351 | + } |
| 352 | + |
| 353 | + CreateNewRoleOrAddPermissions("test", []string{"a", "b"}, &map[string]interface{}{}) |
| 354 | + AddRoleToUser("userId", "test", &map[string]interface{}{}) |
| 355 | + |
| 356 | + res := fakeRes{} |
| 357 | + sessionContainer, err := session.CreateNewSession(res, "userId", map[string]interface{}{}, map[string]interface{}{}) |
| 358 | + assert.NoError(t, err) |
| 359 | + |
| 360 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 361 | + userrolesclaims.PermissionClaimValidators.Includes("a", nil, nil), |
| 362 | + }) |
| 363 | + assert.NoError(t, err) |
| 364 | + |
| 365 | + err = sessionContainer.AssertClaims([]claims.SessionClaimValidator{ |
| 366 | + userrolesclaims.PermissionClaimValidators.Includes("nope", nil, nil), |
| 367 | + }) |
| 368 | + assert.NotNil(t, err) |
| 369 | + assert.IsType(t, sessErrors.InvalidClaimError{}, err) |
| 370 | + |
| 371 | + invalidClaimErr := err.(sessErrors.InvalidClaimError) |
| 372 | + assert.Equal(t, 1, len(invalidClaimErr.InvalidClaims)) |
| 373 | + assert.Equal(t, "st-perm", invalidClaimErr.InvalidClaims[0].ID) |
| 374 | + assert.Equal(t, map[string]interface{}{ |
| 375 | + "actualValue": []interface{}{"a", "b"}, |
| 376 | + "expectedToInclude": "nope", |
| 377 | + "message": "wrong value", |
| 378 | + }, invalidClaimErr.InvalidClaims[0].Reason) |
| 379 | +} |
0 commit comments