github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/user_test.go (about)

     1  package servicecontext
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/evergreen-ci/evergreen/db"
     8  	"github.com/evergreen-ci/evergreen/model/user"
     9  	"github.com/evergreen-ci/evergreen/testutil"
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func TestFindUserById(t *testing.T) {
    14  	testutil.ConfigureIntegrationTest(t, testConfig, "TestFindUserById")
    15  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    16  
    17  	serviceContext := &DBServiceContext{}
    18  	numUsers := 10
    19  
    20  	Convey("When there are user documents in the database", t, func() {
    21  		testutil.HandleTestingErr(db.Clear(user.Collection), t, "Error clearing"+
    22  			" '%v' collection", user.Collection)
    23  		for i := 0; i < numUsers; i++ {
    24  			testUser := &user.DBUser{
    25  				Id:     fmt.Sprintf("user_%d", i),
    26  				APIKey: fmt.Sprintf("apikey_%d", i),
    27  			}
    28  			So(testUser.Insert(), ShouldBeNil)
    29  		}
    30  
    31  		Convey("then properly finding each user should succeed", func() {
    32  			for i := 0; i < numUsers; i++ {
    33  				found, err := serviceContext.FindUserById(fmt.Sprintf("user_%d", i))
    34  				So(err, ShouldBeNil)
    35  				So(found.GetAPIKey(), ShouldEqual, fmt.Sprintf("apikey_%d", i))
    36  			}
    37  		})
    38  		Convey("then searching for user that doesn't exist should return nil",
    39  			func() {
    40  				found, err := serviceContext.FindUserById("fake_user")
    41  				So(err, ShouldBeNil)
    42  				So(found, ShouldBeNil)
    43  			})
    44  	})
    45  }