github.com/minio/console@v1.3.0/pkg/subnet/config_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package subnet
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/minio/pkg/v2/licverifier"
    24  )
    25  
    26  var (
    27  	license = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJrYW5hZ2FyYWorYzFAbWluaW8uaW8iLCJjYXAiOjUwLCJvcmciOiJHcmluZ290dHMgSW5jLiIsImV4cCI6MS42NDE0NDYxNjkwMDExOTg4OTRlOSwicGxhbiI6IlNUQU5EQVJEIiwiaXNzIjoic3VibmV0QG1pbi5pbyIsImFpZCI6MSwiaWF0IjoxLjYwOTkxMDE2OTAwMTE5ODg5NGU5fQ.EhTL2xwMHnUoLQF4UR-5bjUCja3whseLU5mb9XEj7PvAae6HEIDCOMEF8Hhh20DN_v_LRE283j2ZlA5zulcXSZXS0CLcrKqbVy6QLvZfvvLuerOjJI-NBa9dSJWJ0WoN"
    28  
    29  	publicKeys = []string{`-----BEGIN PUBLIC KEY-----
    30  MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEbo+e1wpBY4tBq9AONKww3Kq7m6QP/TBQ
    31  mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU
    32  9NKlTdQFGF3+aO6jbQ4hX/S5qPyF+a3z
    33  -----END PUBLIC KEY-----`}
    34  )
    35  
    36  func TestGetLicenseInfoFromJWT(t *testing.T) {
    37  	mockLicense, _ := GetLicenseInfoFromJWT(license, publicKeys)
    38  
    39  	type args struct {
    40  		license    string
    41  		publicKeys []string
    42  	}
    43  	tests := []struct {
    44  		name    string
    45  		args    args
    46  		want    *licverifier.LicenseInfo
    47  		wantErr bool
    48  	}{
    49  		{
    50  			name: "error because missing license",
    51  			args: args{
    52  				license:    "",
    53  				publicKeys: OfflinePublicKeys,
    54  			},
    55  			wantErr: true,
    56  		},
    57  		{
    58  			name: "error because invalid license",
    59  			args: args{
    60  				license:    license,
    61  				publicKeys: []string{"eaeaeae"},
    62  			},
    63  			wantErr: true,
    64  		},
    65  		{
    66  			name: "license successfully verified",
    67  			args: args{
    68  				license:    license,
    69  				publicKeys: publicKeys,
    70  			},
    71  			wantErr: false,
    72  			want:    mockLicense,
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			got, err := GetLicenseInfoFromJWT(tt.args.license, tt.args.publicKeys)
    78  			if !tt.wantErr {
    79  				t.Skip()
    80  			}
    81  			if (err != nil) != tt.wantErr {
    82  				t.Errorf("GetLicenseInfoFromJWT() error = %v, wantErr %v", err, tt.wantErr)
    83  				return
    84  			}
    85  			if !reflect.DeepEqual(got, tt.want) {
    86  				t.Errorf("GetLicenseInfoFromJWT() got = %v, want %v", got, tt.want)
    87  			}
    88  		})
    89  	}
    90  }