github.com/resonatecoop/id@v1.1.0-43/oauth/suite_test.go (about)

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