github.com/glide-im/glide@v1.6.0/pkg/auth/jwt_auth/auth_test.go (about)

     1  package jwt_auth
     2  
     3  import (
     4  	"github.com/glide-im/glide/pkg/auth"
     5  	"github.com/stretchr/testify/assert"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestJwtAuthorize_Auth(t *testing.T) {
    11  	type args struct {
    12  		c auth.Info
    13  		t *auth.Token
    14  	}
    15  	tests := []struct {
    16  		name    string
    17  		args    args
    18  		want    *auth.Result
    19  		wantErr bool
    20  	}{
    21  		{
    22  			name:    "nil token",
    23  			args:    args{},
    24  			wantErr: true,
    25  		},
    26  		{
    27  			name: "invalid token",
    28  			args: args{
    29  				c: JwtAuthInfo{
    30  					UID:    "",
    31  					Device: "",
    32  				},
    33  				t: &auth.Token{},
    34  			},
    35  			wantErr: true,
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			a := JwtAuthorize{}
    41  			got, err := a.Auth(tt.args.c, tt.args.t)
    42  			if (err != nil) != tt.wantErr {
    43  				t.Errorf("Auth() error = %v, wantErr %v", err, tt.wantErr)
    44  				return
    45  			}
    46  			if !reflect.DeepEqual(got, tt.want) {
    47  				t.Errorf("Auth() got = %v, want %v", got, tt.want)
    48  			}
    49  		})
    50  	}
    51  }
    52  
    53  func TestJwtAuthorize_GetToken(t *testing.T) {
    54  	info := JwtAuthInfo{
    55  		UID:    "3",
    56  		Device: "4",
    57  	}
    58  
    59  	impl := NewAuthorizeImpl("secret")
    60  
    61  	token, err := impl.GetToken(&info)
    62  	assert.Nil(t, err)
    63  	assert.NotNil(t, token)
    64  
    65  	type args struct {
    66  		c auth.Info
    67  	}
    68  	tests := []struct {
    69  		name    string
    70  		args    args
    71  		want    *auth.Token
    72  		wantErr bool
    73  	}{
    74  		{
    75  			name: "nil token",
    76  			args: args{
    77  				c: &info,
    78  			},
    79  			want: token,
    80  		},
    81  		{
    82  			name: "invalid token",
    83  			args: args{
    84  				c: JwtAuthInfo{
    85  					UID:    "",
    86  					Device: "",
    87  				},
    88  			},
    89  			want:    nil,
    90  			wantErr: true,
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			a := JwtAuthorize{}
    96  			got, err := a.GetToken(tt.args.c)
    97  			if (err != nil) != tt.wantErr {
    98  				t.Errorf("GetToken() error = %v, wantErr %v", err, tt.wantErr)
    99  				return
   100  			}
   101  			if !reflect.DeepEqual(got, tt.want) {
   102  				t.Errorf("GetToken() got = %v, want %v", got, tt.want)
   103  			}
   104  		})
   105  	}
   106  }