github.com/litesolutions/justifay-api@v1.0.0-2.0.20220707114139-46f28a909481/server/suite_test.go (about)

     1  package server_test
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"flag"
     7  	"log"
     8  	"os"
     9  	"testing"
    10  
    11  	//"github.com/resonatecoop/id/log"
    12  	// "github.com/resonatecoop/id/log"
    13  	"github.com/litesolutions/justifay-api/model"
    14  	"github.com/litesolutions/justifay-api/pkg/config"
    15  	"github.com/litesolutions/justifay-api/server"
    16  	"github.com/stretchr/testify/suite"
    17  	"github.com/uptrace/bun"
    18  	"github.com/uptrace/bun/dialect/pgdialect"
    19  	"github.com/uptrace/bun/extra/bundebug"
    20  )
    21  
    22  var (
    23  // testDbUser = "go_oauth2_server"
    24  // testDbName = "go_oauth2_server_oauth_test"
    25  
    26  // testFixtures = []string{
    27  // 	"./oauth/fixtures/scopes.yml",
    28  // 	"./oauth/fixtures/roles.yml",
    29  // 	"./oauth/fixtures/test_clients.yml",
    30  // 	"./oauth/fixtures/test_users.yml",
    31  // }
    32  
    33  // 	testMigrations = []func(*bun.DB) error{
    34  // 		model.MigrateAll,
    35  // 	}
    36  )
    37  
    38  func init() {
    39  	if err := os.Chdir("../"); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  
    44  // UserApiTestSuite needs to be exported so the tests run
    45  type UserApiTestSuite struct {
    46  	suite.Suite
    47  	cfg    *config.Configuration
    48  	db     *bun.DB
    49  	ctx    context.Context
    50  	server *server.Server
    51  	// service *oauth.Service
    52  	// clients []*model.Client
    53  	// users   []*model.User
    54  	// router  *mux.Router
    55  }
    56  
    57  // The SetupSuite method will be run by testify once, at the very
    58  // start of the testing suite, before any tests are run.
    59  func (suite *UserApiTestSuite) SetupSuite() {
    60  	// Initialise the config
    61  	cfgPath := flag.String("p", "./conf.local.yaml", "Path to config file")
    62  	flag.Parse()
    63  
    64  	cfg, err := config.Load(*cfgPath)
    65  
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	sqldb, err := sql.Open("pgx", cfg.DB.Dev.PSN)
    71  
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	db := bun.NewDB(sqldb, pgdialect.New())
    77  
    78  	db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
    79  
    80  	suite.cfg = cfg
    81  	suite.ctx = context.Background()
    82  	suite.db = db
    83  
    84  	suite.server = server.New(db)
    85  
    86  	// if err != nil {
    87  	// 	panic(err)
    88  	// }
    89  
    90  	// ASSUME THAT TEST DATABASE HAS ALREADY BEEN CREATED
    91  	// Create the test database
    92  	// db, err := testutil.CreateTestDatabasePostgres(
    93  	// 	suite.cnf.Database.Host,
    94  	// 	testDbUser,
    95  	// 	testDbName,
    96  	// 	testMigrations,
    97  	// 	testFixtures,
    98  	// )
    99  	// if err != nil {
   100  	// 	t.Skip(err)
   101  	// }
   102  	// suite.db = db
   103  	// suite.db2 = nil // TODO setup test mysql db client
   104  
   105  	// Fetch test client
   106  	// suite.clients = make([]*model.Client, 0)
   107  
   108  	// rows, err := suite.db.QueryContext(ctx, "SELECT * from clients ORDER BY created_at")
   109  
   110  	// if err != nil {
   111  	// 	panic(err)
   112  	// }
   113  
   114  	// err = suite.db.ScanRows(ctx, rows, &suite.clients)
   115  
   116  	// if err != nil {
   117  	// 	log.ERROR.Fatal(err)
   118  	// }
   119  
   120  	// // Fetch test users
   121  	// suite.users = make([]*model.User, 0)
   122  
   123  	// rows, err = suite.db.QueryContext(ctx, "SELECT * from users ORDER BY created_at")
   124  
   125  	// if err != nil {
   126  	// 	panic(err)
   127  	// }
   128  
   129  	// err = suite.db.ScanRows(ctx, rows, &suite.users)
   130  
   131  	// if err != nil {
   132  	// 	log.ERROR.Fatal(err)
   133  	// }
   134  	// // Initialise the service
   135  	// suite.service = oauth.NewService(suite.cnf, suite.db)
   136  
   137  	// // Register routes
   138  	// suite.router = mux.NewRouter()
   139  	// suite.service.RegisterRoutes(suite.router, "/v1/oauth")
   140  
   141  	// return nil
   142  }
   143  
   144  // The TearDownSuite method will be run by testify once, at the very
   145  // end of the testing suite, after all tests have been run.
   146  func (suite *UserApiTestSuite) TearDownSuite() {
   147  	//
   148  }
   149  
   150  // The SetupTest method will be run before every test in the suite.
   151  func (suite *UserApiTestSuite) SetupTest() {
   152  	//
   153  }
   154  
   155  // The TearDownTest method will be run after every test in the suite.
   156  func (suite *UserApiTestSuite) TearDownTest() {
   157  	// Scopes are static, populated from fixtures,
   158  	// so there is no need to clear them after running a test
   159  
   160  	suite.db.NewTruncateTable().
   161  		Model(new(model.AuthorizationCode)).
   162  		Exec(suite.ctx)
   163  
   164  	suite.db.NewTruncateTable().
   165  		Model(new(model.RefreshToken)).
   166  		Exec(suite.ctx)
   167  
   168  	suite.db.NewTruncateTable().
   169  		Model(new(model.AccessToken)).
   170  		Exec(suite.ctx)
   171  
   172  	ids := []string{
   173  		"243b4178-6f98-4bf1-bbb1-46b57a901816",
   174  		"5253747c-2b8c-40e2-8a70-bab91348a9bd",
   175  		"90b26113-37e0-456a-9f75-01db0eb550f8",
   176  		"f40cf437-eef2-4659-8eb3-7ee93f6dfcea",
   177  		"046e7a23-a9f0-4fb8-a60f-861b2df05d95",
   178  		"953c3537-3149-4164-9c73-ec785a3c17c4"}
   179  
   180  	suite.db.NewDelete().
   181  		Model(new(model.User)).
   182  		Where("id NOT IN (?)", bun.In(ids)).
   183  		ForceDelete().
   184  		Exec(suite.ctx)
   185  
   186  	suite.db.NewUpdate().
   187  		Model(new(model.User)).
   188  		Set("deleted_at = NULL").
   189  		WhereAllWithDeleted().
   190  		Where("deleted_at IS NOT NULL").
   191  		Exec(suite.ctx)
   192  
   193  	ids = []string{"3392e754-ba3e-424f-a687-add9a8ab39c9", "295be195-898c-4f0c-b6a0-8c62105f42de"}
   194  
   195  	suite.db.NewDelete().
   196  		Model(new(model.Client)).
   197  		Where("id NOT IN (?)", bun.In(ids)).
   198  		ForceDelete().
   199  		Exec(suite.ctx)
   200  }
   201  
   202  // TestUserApiTestSuite ...
   203  // In order for 'go test' to run this suite, we need to create
   204  // a normal test function and pass our suite to suite.Run
   205  func TestUserApiTestSuite(t *testing.T) {
   206  	suite.Run(t, new(UserApiTestSuite))
   207  }