git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/otp/otp_test.go (about) 1 /** 2 * Copyright 2014 Paul Querna 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package otp 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestKeyAllThere(t *testing.T) { 27 k, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example&algorithm=sha256&digits=8`) 28 require.NoError(t, err, "failed to parse url") 29 require.Equal(t, "totp", k.Type(), "Extracting Type") 30 require.Equal(t, "Example", k.Issuer(), "Extracting Issuer") 31 require.Equal(t, "alice@google.com", k.AccountName(), "Extracting Account Name") 32 require.Equal(t, "JBSWY3DPEHPK3PXP", k.Secret(), "Extracting Secret") 33 require.Equal(t, AlgorithmSHA256, k.Algorithm()) 34 require.Equal(t, DigitsEight, k.Digits()) 35 } 36 37 func TestKeyIssuerOnlyInPath(t *testing.T) { 38 k, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP`) 39 require.NoError(t, err, "failed to parse url") 40 require.Equal(t, "Example", k.Issuer(), "Extracting Issuer") 41 require.Equal(t, "alice@google.com", k.AccountName(), "Extracting Account Name") 42 } 43 44 func TestKeyNoIssuer(t *testing.T) { 45 k, err := NewKeyFromURL(`otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP`) 46 require.NoError(t, err, "failed to parse url") 47 require.Equal(t, "", k.Issuer(), "Extracting Issuer") 48 require.Equal(t, "alice@google.com", k.AccountName(), "Extracting Account Name") 49 } 50 51 func TestKeyWithNewLine(t *testing.T) { 52 w, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP 53 `) 54 require.NoError(t, err) 55 sec := w.Secret() 56 require.Equal(t, "JBSWY3DPEHPK3PXP", sec) 57 }