From 32e4e0d8358cc8881f0bd46a3c43bb218e6682b3 Mon Sep 17 00:00:00 2001 From: Joshua Casey Date: Wed, 8 May 2024 22:39:16 -0500 Subject: [PATCH] Add TestGenerateOTPCode --- internal/testutil/totp/totp_test.go | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 internal/testutil/totp/totp_test.go diff --git a/internal/testutil/totp/totp_test.go b/internal/testutil/totp/totp_test.go new file mode 100644 index 000000000..5ff32bdf8 --- /dev/null +++ b/internal/testutil/totp/totp_test.go @@ -0,0 +1,44 @@ +package totp + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestGenerateOTPCode(t *testing.T) { + tests := []struct { + name string + token string + when time.Time + wantCode string + wantRemainingLifetimeSeconds int64 + }{ + { + name: "Use a token from online example", + token: "JBSWY3DPEHPK3PXP", // https://github.com/pquerna/otp/blob/3357de7c04813a328d6a1e4a514854213e0f8ce8/totp/totp.go#L180 + when: time.Unix(1715205169, 0), + wantCode: "780919", + wantRemainingLifetimeSeconds: 11, + }, + { + name: "Use a token that was randomly generated", + token: "EDAYKXL3TEYZNQ3O4N5KPSUAQQLZYUJG", + when: time.Unix(1715225917, 0), + wantCode: "920615", + wantRemainingLifetimeSeconds: 23, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + actualCode, actualRemainingLifetimeSeconds := GenerateOTPCode(t, test.token, test.when) + + require.Equal(t, test.wantCode, actualCode) + require.Equal(t, test.wantRemainingLifetimeSeconds, actualRemainingLifetimeSeconds) + }) + } +}