github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/user_factory_test.go (about) 1 package db_test 2 3 import ( 4 "encoding/base64" 5 "time" 6 7 "github.com/pf-qiu/concourse/v6/atc/db" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("User Factory", func() { 14 15 var ( 16 err error 17 users []db.User 18 ) 19 20 JustBeforeEach(func() { 21 err = userFactory.CreateOrUpdateUser("test", "github", 22 base64.StdEncoding.EncodeToString([]byte("test"+"github"))) 23 Expect(err).ToNot(HaveOccurred()) 24 25 users, err = userFactory.GetAllUsers() 26 Expect(err).ToNot(HaveOccurred()) 27 }) 28 29 Context("when user doesn't exist", func() { 30 It("Insert a user with last_login now()", func() { 31 Expect(users).To(HaveLen(1)) 32 Expect(users[0].Name()).To(Equal("test")) 33 Expect(users[0].LastLogin()).Should(BeTemporally("~", time.Now(), time.Second*20)) 34 }) 35 }) 36 37 Context("when username exists but with different connector", func() { 38 BeforeEach(func() { 39 err = userFactory.CreateOrUpdateUser("test", "basic", 40 base64.StdEncoding.EncodeToString([]byte("test"+"basic"))) 41 Expect(err).ToNot(HaveOccurred()) 42 }) 43 44 It("Creates a different user", func() { 45 Expect(users).To(HaveLen(2)) 46 Expect(users[0].ID()).ToNot(Equal(users[1].ID())) 47 }) 48 }) 49 50 Context("when username exists and with the same connector", func() { 51 var previousLastLogin time.Time 52 53 BeforeEach(func() { 54 err = userFactory.CreateOrUpdateUser("test", "github", 55 base64.StdEncoding.EncodeToString([]byte("test"+"github"))) 56 Expect(err).ToNot(HaveOccurred()) 57 58 users, err = userFactory.GetAllUsers() 59 Expect(err).ToNot(HaveOccurred()) 60 previousLastLogin = users[0].LastLogin() 61 }) 62 63 It("Doesn't create a different user", func() { 64 Expect(users).To(HaveLen(1)) 65 }) 66 67 It("Doesn't create a new record", func() { 68 Expect(users[0].Name()).To(Equal("test")) 69 }) 70 71 It("Update the last_login time", func() { 72 Expect(users[0].LastLogin()).NotTo(Equal(previousLastLogin)) 73 }) 74 }) 75 })