(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestGenerateOTP(t *testing.T) { |
| 11 | t.Run("should return 6-character OTP", func(t *testing.T) { |
| 12 | otp, err := GenerateOTP() |
| 13 | require.NoError(t, err) |
| 14 | assert.Len(t, otp, 6) |
| 15 | }) |
| 16 | |
| 17 | t.Run("should only contain valid charset characters", func(t *testing.T) { |
| 18 | charset := "ABCDEFGHJKLMNPQRSTUVWXYZ123456789" |
| 19 | for i := 0; i < 50; i++ { |
| 20 | otp, err := GenerateOTP() |
| 21 | require.NoError(t, err) |
| 22 | for _, c := range otp { |
| 23 | assert.Contains(t, charset, string(c), "OTP contains invalid character: %c", c) |
| 24 | } |
| 25 | } |
| 26 | }) |
| 27 | |
| 28 | t.Run("should generate unique OTPs", func(t *testing.T) { |
| 29 | seen := make(map[string]bool) |
| 30 | duplicates := 0 |
| 31 | for i := 0; i < 100; i++ { |
| 32 | otp, err := GenerateOTP() |
| 33 | require.NoError(t, err) |
| 34 | if seen[otp] { |
| 35 | duplicates++ |
| 36 | } |
| 37 | seen[otp] = true |
| 38 | } |
| 39 | // With crypto/rand and 33^6 possibilities, duplicates should be extremely rare |
| 40 | assert.LessOrEqual(t, duplicates, 2, "too many duplicate OTPs generated") |
| 41 | }) |
| 42 | } |
nothing calls this directly
no test coverage detected