github.com/caputomarcos/go-oauth2-server@v1.0.1/oauth/suite_test.go (about) 1 package oauth_test 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/RichardKnop/go-oauth2-server/config" 8 "github.com/RichardKnop/go-oauth2-server/log" 9 "github.com/RichardKnop/go-oauth2-server/models" 10 "github.com/RichardKnop/go-oauth2-server/oauth" 11 "github.com/RichardKnop/go-oauth2-server/test-util" 12 "github.com/gorilla/mux" 13 "github.com/jinzhu/gorm" 14 "github.com/stretchr/testify/suite" 15 ) 16 17 var ( 18 testDbUser = "go_oauth2_server" 19 testDbName = "go_oauth2_server_oauth_test" 20 21 testFixtures = []string{ 22 "./oauth/fixtures/scopes.yml", 23 "./oauth/fixtures/roles.yml", 24 "./oauth/fixtures/test_clients.yml", 25 "./oauth/fixtures/test_users.yml", 26 } 27 28 testMigrations = []func(*gorm.DB) error{ 29 models.MigrateAll, 30 } 31 ) 32 33 func init() { 34 if err := os.Chdir("../"); err != nil { 35 log.ERROR.Fatal(err) 36 } 37 } 38 39 // OauthTestSuite needs to be exported so the tests run 40 type OauthTestSuite struct { 41 suite.Suite 42 cnf *config.Config 43 db *gorm.DB 44 service *oauth.Service 45 clients []*models.OauthClient 46 users []*models.OauthUser 47 router *mux.Router 48 } 49 50 // The SetupSuite method will be run by testify once, at the very 51 // start of the testing suite, before any tests are run. 52 func (suite *OauthTestSuite) SetupSuite() { 53 // Initialise the config 54 suite.cnf = config.NewConfig(false, false, "etcd") 55 56 // Create the test database 57 db, err := testutil.CreateTestDatabasePostgres( 58 suite.cnf.Database.Host, 59 testDbUser, 60 testDbName, 61 testMigrations, 62 testFixtures, 63 ) 64 if err != nil { 65 log.ERROR.Fatal(err) 66 } 67 suite.db = db 68 69 // Fetch test client 70 suite.clients = make([]*models.OauthClient, 0) 71 if err := suite.db.Order("created_at").Find(&suite.clients).Error; err != nil { 72 log.ERROR.Fatal(err) 73 } 74 75 // Fetch test users 76 suite.users = make([]*models.OauthUser, 0) 77 if err := suite.db.Order("created_at").Find(&suite.users).Error; err != nil { 78 log.ERROR.Fatal(err) 79 } 80 81 // Initialise the service 82 suite.service = oauth.NewService(suite.cnf, suite.db) 83 84 // Register routes 85 suite.router = mux.NewRouter() 86 suite.service.RegisterRoutes(suite.router, "/v1/oauth") 87 } 88 89 // The TearDownSuite method will be run by testify once, at the very 90 // end of the testing suite, after all tests have been run. 91 func (suite *OauthTestSuite) TearDownSuite() { 92 // 93 } 94 95 // The SetupTest method will be run before every test in the suite. 96 func (suite *OauthTestSuite) SetupTest() { 97 // 98 } 99 100 // The TearDownTest method will be run after every test in the suite. 101 func (suite *OauthTestSuite) TearDownTest() { 102 // Scopes are static, populated from fixtures, 103 // so there is no need to clear them after running a test 104 suite.db.Unscoped().Delete(new(models.OauthAuthorizationCode)) 105 suite.db.Unscoped().Delete(new(models.OauthRefreshToken)) 106 suite.db.Unscoped().Delete(new(models.OauthAccessToken)) 107 suite.db.Unscoped().Not("id", []string{"1", "2"}).Delete(new(models.OauthUser)) 108 suite.db.Unscoped().Not("id", []string{"1", "2", "3"}).Delete(new(models.OauthClient)) 109 } 110 111 // TestOauthTestSuite ... 112 // In order for 'go test' to run this suite, we need to create 113 // a normal test function and pass our suite to suite.Run 114 func TestOauthTestSuite(t *testing.T) { 115 suite.Run(t, new(OauthTestSuite)) 116 }