github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/oauth/suite_test.go (about)

     1  package oauth_test
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/gorilla/mux"
     9  	"github.com/jinzhu/gorm"
    10  	"github.com/stretchr/testify/suite"
    11  	"github.com/wanliu/go-oauth2-server/config"
    12  	"github.com/wanliu/go-oauth2-server/models"
    13  	"github.com/wanliu/go-oauth2-server/oauth"
    14  	"github.com/wanliu/go-oauth2-server/test-util"
    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.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  		testDbUser,
    59  		testDbName,
    60  		testMigrations,
    61  		testFixtures,
    62  	)
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	suite.db = db
    67  
    68  	// Fetch test client
    69  	suite.clients = make([]*models.OauthClient, 0)
    70  	if err := suite.db.Order("created_at").Find(&suite.clients).Error; err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	// Fetch test users
    75  	suite.users = make([]*models.OauthUser, 0)
    76  	if err := suite.db.Order("created_at").Find(&suite.users).Error; err != nil {
    77  		log.Fatal(err)
    78  	}
    79  
    80  	// Initialise the service
    81  	suite.service = oauth.NewService(suite.cnf, suite.db)
    82  
    83  	// Register routes
    84  	suite.router = mux.NewRouter()
    85  	suite.service.RegisterRoutes(suite.router, "/v1/oauth")
    86  }
    87  
    88  // The TearDownSuite method will be run by testify once, at the very
    89  // end of the testing suite, after all tests have been run.
    90  func (suite *OauthTestSuite) TearDownSuite() {
    91  	//
    92  }
    93  
    94  // The SetupTest method will be run before every test in the suite.
    95  func (suite *OauthTestSuite) SetupTest() {
    96  	//
    97  }
    98  
    99  // The TearDownTest method will be run after every test in the suite.
   100  func (suite *OauthTestSuite) TearDownTest() {
   101  	// Scopes are static, populated from fixtures,
   102  	// so there is no need to clear them after running a test
   103  	suite.db.Unscoped().Delete(new(models.OauthAuthorizationCode))
   104  	suite.db.Unscoped().Delete(new(models.OauthRefreshToken))
   105  	suite.db.Unscoped().Delete(new(models.OauthAccessToken))
   106  	suite.db.Unscoped().Not("id", []string{"1", "2"}).Delete(new(models.OauthUser))
   107  	suite.db.Unscoped().Not("id", []string{"1", "2", "3"}).Delete(new(models.OauthClient))
   108  }
   109  
   110  // TestOauthTestSuite ...
   111  // In order for 'go test' to run this suite, we need to create
   112  // a normal test function and pass our suite to suite.Run
   113  func TestOauthTestSuite(t *testing.T) {
   114  	suite.Run(t, new(OauthTestSuite))
   115  }