|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package emailverification |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/supertokens/supertokens-golang/recipe/emailverification/evclaims" |
| 25 | +) |
| 26 | + |
| 27 | +func TestEmailVerificationClaim(t *testing.T) { |
| 28 | + t.Run("value should be fetched if it is nil", func(t *testing.T) { |
| 29 | + validator := evclaims.EmailVerificationClaimValidators.IsVerified(nil, nil) |
| 30 | + |
| 31 | + shouldRefreshNil := validator.ShouldRefetch(nil, nil) |
| 32 | + |
| 33 | + assert.True(t, shouldRefreshNil) |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("value should be fetched as per maxAgeInSeconds if it is provided", func(t *testing.T) { |
| 37 | + refetchTimeOnFalseInSeconds := int64(10) |
| 38 | + maxAgeInSeconds := int64(200) |
| 39 | + validator := evclaims.EmailVerificationClaimValidators.IsVerified(&refetchTimeOnFalseInSeconds, &maxAgeInSeconds) |
| 40 | + |
| 41 | + payload := map[string]interface{}{ |
| 42 | + "st-ev": map[string]interface{}{ |
| 43 | + "v": true, |
| 44 | + "t": time.Now().UnixMilli() - 199*1000, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + shouldRefreshValid := validator.ShouldRefetch(payload, nil) |
| 49 | + |
| 50 | + assert.False(t, shouldRefreshValid) |
| 51 | + |
| 52 | + payload = map[string]interface{}{ |
| 53 | + "st-ev": map[string]interface{}{ |
| 54 | + "v": true, |
| 55 | + "t": time.Now().UnixMilli() - 201*1000, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + shouldRefreshExpired := validator.ShouldRefetch(payload, nil) |
| 60 | + assert.True(t, shouldRefreshExpired) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("value should be fetched as per refetchTimeOnFalseInSeconds if it is provided", func(t *testing.T) { |
| 64 | + refetchTimeOnFalseInSeconds := int64(8) |
| 65 | + validator := evclaims.EmailVerificationClaimValidators.IsVerified(&refetchTimeOnFalseInSeconds, nil) |
| 66 | + |
| 67 | + payload := map[string]interface{}{ |
| 68 | + "st-ev": map[string]interface{}{ |
| 69 | + "v": false, |
| 70 | + "t": time.Now().UnixMilli() - 7*1000, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + shouldRefreshValid := validator.ShouldRefetch(payload, nil) |
| 75 | + |
| 76 | + assert.False(t, shouldRefreshValid) |
| 77 | + |
| 78 | + payload = map[string]interface{}{ |
| 79 | + "st-ev": map[string]interface{}{ |
| 80 | + "v": false, |
| 81 | + "t": time.Now().UnixMilli() - 9*1000, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + shouldRefreshExpired := validator.ShouldRefetch(payload, nil) |
| 86 | + assert.True(t, shouldRefreshExpired) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("value should be fetched as per default the refetchTimeOnFalseInSeconds if it is not provided", func(t *testing.T) { |
| 90 | + validator := evclaims.EmailVerificationClaimValidators.IsVerified(nil, nil) |
| 91 | + |
| 92 | + // NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds |
| 93 | + payload := map[string]interface{}{ |
| 94 | + "st-ev": map[string]interface{}{ |
| 95 | + "v": false, |
| 96 | + "t": time.Now().UnixMilli() - 9*1000, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + shouldRefreshValid := validator.ShouldRefetch(payload, nil) |
| 101 | + |
| 102 | + assert.False(t, shouldRefreshValid) |
| 103 | + |
| 104 | + payload = map[string]interface{}{ |
| 105 | + "st-ev": map[string]interface{}{ |
| 106 | + "v": false, |
| 107 | + "t": time.Now().UnixMilli() - 11*1000, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + shouldRefreshExpired := validator.ShouldRefetch(payload, nil) |
| 112 | + assert.True(t, shouldRefreshExpired) |
| 113 | + }) |
| 114 | +} |
0 commit comments