...

Source file src/github.com/pquerna/otp/otp_test.go

Documentation: github.com/pquerna/otp

     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  	"github.com/stretchr/testify/require"
    22  
    23  	"testing"
    24  )
    25  
    26  func TestKeyAllThere(t *testing.T) {
    27  	k, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example`)
    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  }
    34  
    35  func TestKeyIssuerOnlyInPath(t *testing.T) {
    36  	k, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP`)
    37  	require.NoError(t, err, "failed to parse url")
    38  	require.Equal(t, "Example", k.Issuer(), "Extracting Issuer")
    39  	require.Equal(t, "alice@google.com", k.AccountName(), "Extracting Account Name")
    40  }
    41  
    42  func TestKeyNoIssuer(t *testing.T) {
    43  	k, err := NewKeyFromURL(`otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP`)
    44  	require.NoError(t, err, "failed to parse url")
    45  	require.Equal(t, "", k.Issuer(), "Extracting Issuer")
    46  	require.Equal(t, "alice@google.com", k.AccountName(), "Extracting Account Name")
    47  }
    48  
    49  func TestKeyWithNewLine(t *testing.T) {
    50  	w, err := NewKeyFromURL(`otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP
    51  `)
    52  	require.NoError(t, err)
    53  	sec := w.Secret()
    54  	require.Equal(t, "JBSWY3DPEHPK3PXP", sec)
    55  }
    56  

View as plain text