github.com/volatiletech/authboss@v2.4.1+incompatible/user_test.go (about)

     1  package authboss
     2  
     3  import "testing"
     4  
     5  func TestOAuth2PIDs(t *testing.T) {
     6  	t.Parallel()
     7  
     8  	provider := "provider"
     9  	uid := "uid"
    10  	pid := MakeOAuth2PID(provider, uid)
    11  
    12  	if pid != "oauth2;;provider;;uid" {
    13  		t.Error("pid was wrong:", pid)
    14  	}
    15  
    16  	gotProvider, gotUID := ParseOAuth2PIDP(pid)
    17  	if gotUID != uid {
    18  		t.Error("uid was wrong:", gotUID)
    19  	}
    20  	if gotProvider != provider {
    21  		t.Error("provider was wrong:", gotProvider)
    22  	}
    23  
    24  	notEnoughSegments, didntStartWithOAuth2 := false, false
    25  
    26  	func() {
    27  		defer func() {
    28  			if r := recover(); r != nil {
    29  				notEnoughSegments = true
    30  			}
    31  		}()
    32  
    33  		_, _ = ParseOAuth2PIDP("nope")
    34  	}()
    35  
    36  	if !notEnoughSegments {
    37  		t.Error("expected a panic when there's not enough segments")
    38  	}
    39  
    40  	func() {
    41  		defer func() {
    42  			if r := recover(); r != nil {
    43  				didntStartWithOAuth2 = true
    44  			}
    45  		}()
    46  
    47  		_, _ = ParseOAuth2PIDP("notoauth2;;but;;restisgood")
    48  	}()
    49  
    50  	if !didntStartWithOAuth2 {
    51  		t.Error("expected a panic when the pid doesn't start with oauth2")
    52  	}
    53  }
    54  
    55  type testAssertionFailUser struct{}
    56  
    57  func (testAssertionFailUser) GetPID() string { return "" }
    58  func (testAssertionFailUser) PutPID(string)  {}
    59  
    60  func TestUserAssertions(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	u := &mockUser{}
    64  	fu := testAssertionFailUser{}
    65  
    66  	paniced := false
    67  	func() {
    68  		defer func() {
    69  			if r := recover(); r != nil {
    70  				paniced = true
    71  			}
    72  		}()
    73  
    74  		MustBeAuthable(u)
    75  		MustBeConfirmable(u)
    76  		MustBeLockable(u)
    77  		MustBeOAuthable(u)
    78  		MustBeRecoverable(u)
    79  	}()
    80  
    81  	if paniced {
    82  		t.Error("The mock user should have included all interfaces and should not panic")
    83  	}
    84  
    85  	didPanic := func(f func()) (paniced bool) {
    86  		defer func() {
    87  			if r := recover(); r != nil {
    88  				paniced = true
    89  			}
    90  		}()
    91  
    92  		f()
    93  		return paniced
    94  	}
    95  
    96  	if !didPanic(func() { MustBeAuthable(fu) }) {
    97  		t.Error("should have panic'd")
    98  	}
    99  	if !didPanic(func() { MustBeConfirmable(fu) }) {
   100  		t.Error("should have panic'd")
   101  	}
   102  	if !didPanic(func() { MustBeLockable(fu) }) {
   103  		t.Error("should have panic'd")
   104  	}
   105  	if !didPanic(func() { MustBeOAuthable(fu) }) {
   106  		t.Error("should have panic'd")
   107  	}
   108  	if !didPanic(func() { MustBeRecoverable(fu) }) {
   109  		t.Error("should have panic'd")
   110  	}
   111  }