github.com/polarismesh/polaris@v1.17.8/auth/defaultauth/main_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package defaultauth_test
    19  
    20  import (
    21  	"errors"
    22  
    23  	_ "github.com/go-sql-driver/mysql"
    24  	bolt "go.etcd.io/bbolt"
    25  
    26  	"github.com/polarismesh/polaris/auth"
    27  	"github.com/polarismesh/polaris/cache"
    28  	_ "github.com/polarismesh/polaris/cache"
    29  	api "github.com/polarismesh/polaris/common/api/v1"
    30  	commonlog "github.com/polarismesh/polaris/common/log"
    31  	"github.com/polarismesh/polaris/namespace"
    32  	"github.com/polarismesh/polaris/plugin"
    33  	_ "github.com/polarismesh/polaris/plugin/cmdb/memory"
    34  	_ "github.com/polarismesh/polaris/plugin/discoverevent/local"
    35  	_ "github.com/polarismesh/polaris/plugin/healthchecker/memory"
    36  	_ "github.com/polarismesh/polaris/plugin/healthchecker/redis"
    37  	_ "github.com/polarismesh/polaris/plugin/history/logger"
    38  	_ "github.com/polarismesh/polaris/plugin/password"
    39  	_ "github.com/polarismesh/polaris/plugin/ratelimit/lrurate"
    40  	_ "github.com/polarismesh/polaris/plugin/ratelimit/token"
    41  	_ "github.com/polarismesh/polaris/plugin/statis/logger"
    42  	_ "github.com/polarismesh/polaris/plugin/statis/prometheus"
    43  	"github.com/polarismesh/polaris/service/healthcheck"
    44  	"github.com/polarismesh/polaris/store"
    45  	"github.com/polarismesh/polaris/store/boltdb"
    46  	_ "github.com/polarismesh/polaris/store/boltdb"
    47  	_ "github.com/polarismesh/polaris/store/mysql"
    48  	sqldb "github.com/polarismesh/polaris/store/mysql"
    49  	testsuit "github.com/polarismesh/polaris/test/suit"
    50  )
    51  
    52  const (
    53  	tblUser     string = "user"
    54  	tblStrategy string = "strategy"
    55  	tblGroup    string = "group"
    56  )
    57  
    58  type Bootstrap struct {
    59  	Logger map[string]*commonlog.Options
    60  }
    61  
    62  type TestConfig struct {
    63  	Bootstrap    Bootstrap          `yaml:"bootstrap"`
    64  	Cache        cache.Config       `yaml:"cache"`
    65  	Namespace    namespace.Config   `yaml:"namespace"`
    66  	HealthChecks healthcheck.Config `yaml:"healthcheck"`
    67  	Store        store.Config       `yaml:"store"`
    68  	Auth         auth.Config        `yaml:"auth"`
    69  	Plugin       plugin.Config      `yaml:"plugin"`
    70  }
    71  
    72  type AuthTestSuit struct {
    73  	testsuit.DiscoverTestSuit
    74  }
    75  
    76  // 判断一个resp是否执行成功
    77  func respSuccess(resp api.ResponseMessage) bool {
    78  
    79  	ret := api.CalcCode(resp) == 200
    80  
    81  	return ret
    82  }
    83  
    84  type options func(cfg *TestConfig)
    85  
    86  func (d *AuthTestSuit) cleanAllUser() {
    87  	if d.Storage.Name() == sqldb.STORENAME {
    88  		func() {
    89  			tx, err := d.Storage.StartTx()
    90  			if err != nil {
    91  				panic(err)
    92  			}
    93  
    94  			dbTx := tx.GetDelegateTx().(*sqldb.BaseTx)
    95  
    96  			defer dbTx.Rollback()
    97  
    98  			if _, err := dbTx.Exec("delete from user where name like 'test%'"); err != nil {
    99  				dbTx.Rollback()
   100  				panic(err)
   101  			}
   102  
   103  			dbTx.Commit()
   104  		}()
   105  	} else if d.Storage.Name() == boltdb.STORENAME {
   106  		func() {
   107  			tx, err := d.Storage.StartTx()
   108  			if err != nil {
   109  				panic(err)
   110  			}
   111  
   112  			dbTx := tx.GetDelegateTx().(*bolt.Tx)
   113  			defer dbTx.Rollback()
   114  
   115  			if err := dbTx.DeleteBucket([]byte(tblUser)); err != nil {
   116  				if !errors.Is(err, bolt.ErrBucketNotFound) {
   117  					panic(err)
   118  				}
   119  			}
   120  
   121  			dbTx.Commit()
   122  		}()
   123  	}
   124  }
   125  
   126  func (d *AuthTestSuit) cleanAllUserGroup() {
   127  	if d.Storage.Name() == sqldb.STORENAME {
   128  		func() {
   129  			tx, err := d.Storage.StartTx()
   130  			if err != nil {
   131  				panic(err)
   132  			}
   133  
   134  			dbTx := tx.GetDelegateTx().(*sqldb.BaseTx)
   135  
   136  			defer dbTx.Rollback()
   137  
   138  			if _, err := dbTx.Exec("delete from user_group where name like 'test%'"); err != nil {
   139  				dbTx.Rollback()
   140  				panic(err)
   141  			}
   142  			if _, err := dbTx.Exec("delete from user_group_relation"); err != nil {
   143  				dbTx.Rollback()
   144  				panic(err)
   145  			}
   146  
   147  			dbTx.Commit()
   148  		}()
   149  	} else if d.Storage.Name() == boltdb.STORENAME {
   150  		func() {
   151  			tx, err := d.Storage.StartTx()
   152  			if err != nil {
   153  				panic(err)
   154  			}
   155  
   156  			dbTx := tx.GetDelegateTx().(*bolt.Tx)
   157  			defer dbTx.Rollback()
   158  
   159  			if err := dbTx.DeleteBucket([]byte(tblGroup)); err != nil {
   160  				if !errors.Is(err, bolt.ErrBucketNotFound) {
   161  					panic(err)
   162  				}
   163  			}
   164  
   165  			dbTx.Commit()
   166  		}()
   167  	}
   168  }
   169  
   170  func (d *AuthTestSuit) cleanAllAuthStrategy() {
   171  	if d.Storage.Name() == sqldb.STORENAME {
   172  		func() {
   173  			tx, err := d.Storage.StartTx()
   174  			if err != nil {
   175  				panic(err)
   176  			}
   177  
   178  			dbTx := tx.GetDelegateTx().(*sqldb.BaseTx)
   179  
   180  			defer dbTx.Rollback()
   181  
   182  			if _, err := dbTx.Exec("delete from auth_strategy where id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
   183  				dbTx.Rollback()
   184  				panic(err)
   185  			}
   186  			if _, err := dbTx.Exec("delete from auth_principal where strategy_id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
   187  				dbTx.Rollback()
   188  				panic(err)
   189  			}
   190  			if _, err := dbTx.Exec("delete from auth_strategy_resource where strategy_id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
   191  				dbTx.Rollback()
   192  				panic(err)
   193  			}
   194  
   195  			dbTx.Commit()
   196  		}()
   197  	} else if d.Storage.Name() == boltdb.STORENAME {
   198  		func() {
   199  			tx, err := d.Storage.StartTx()
   200  			if err != nil {
   201  				panic(err)
   202  			}
   203  
   204  			dbTx := tx.GetDelegateTx().(*bolt.Tx)
   205  			defer dbTx.Rollback()
   206  
   207  			if err := dbTx.DeleteBucket([]byte(tblStrategy)); err != nil {
   208  				if !errors.Is(err, bolt.ErrBucketNotFound) {
   209  					panic(err)
   210  				}
   211  			}
   212  
   213  			dbTx.Commit()
   214  		}()
   215  	}
   216  }