login_sources_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "testing"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. "github.com/stretchr/testify/assert"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. func TestLoginSource_BeforeSave(t *testing.T) {
  13. t.Run("Config has not been set", func(t *testing.T) {
  14. s := &LoginSource{}
  15. err := s.BeforeSave()
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. assert.Empty(t, s.RawConfig)
  20. })
  21. t.Run("Config has been set", func(t *testing.T) {
  22. s := &LoginSource{
  23. Config: &PAMConfig{ServiceName: "pam_service"},
  24. }
  25. err := s.BeforeSave()
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. assert.Equal(t, `{"ServiceName":"pam_service"}`, s.RawConfig)
  30. })
  31. }
  32. func TestLoginSource_BeforeCreate(t *testing.T) {
  33. t.Run("CreatedUnix has been set", func(t *testing.T) {
  34. s := &LoginSource{CreatedUnix: 1}
  35. s.BeforeCreate()
  36. assert.Equal(t, int64(1), s.CreatedUnix)
  37. assert.Equal(t, int64(0), s.UpdatedUnix)
  38. })
  39. t.Run("CreatedUnix has not been set", func(t *testing.T) {
  40. s := &LoginSource{}
  41. s.BeforeCreate()
  42. assert.Equal(t, gorm.NowFunc().Unix(), s.CreatedUnix)
  43. assert.Equal(t, gorm.NowFunc().Unix(), s.UpdatedUnix)
  44. })
  45. }
  46. func Test_loginSources(t *testing.T) {
  47. if testing.Short() {
  48. t.Skip()
  49. }
  50. t.Parallel()
  51. tables := []interface{}{new(LoginSource), new(User)}
  52. db := &loginSources{
  53. DB: initTestDB(t, "loginSources", tables...),
  54. }
  55. for _, tc := range []struct {
  56. name string
  57. test func(*testing.T, *loginSources)
  58. }{
  59. {"Create", test_loginSources_Create},
  60. {"Count", test_loginSources_Count},
  61. {"DeleteByID", test_loginSources_DeleteByID},
  62. {"GetByID", test_loginSources_GetByID},
  63. {"List", test_loginSources_List},
  64. {"ResetNonDefault", test_loginSources_ResetNonDefault},
  65. {"Save", test_loginSources_Save},
  66. } {
  67. t.Run(tc.name, func(t *testing.T) {
  68. t.Cleanup(func() {
  69. err := clearTables(t, db.DB, tables...)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. })
  74. tc.test(t, db)
  75. })
  76. }
  77. }
  78. func test_loginSources_Create(t *testing.T, db *loginSources) {
  79. // Create first login source with name "GitHub"
  80. source, err := db.Create(CreateLoginSourceOpts{
  81. Type: LoginGitHub,
  82. Name: "GitHub",
  83. Activated: true,
  84. Default: false,
  85. Config: &GitHubConfig{
  86. APIEndpoint: "https://api.github.com",
  87. },
  88. })
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. // Get it back and check the Created field
  93. source, err = db.GetByID(source.ID)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), source.Created.UTC().Format(time.RFC3339))
  98. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), source.Updated.UTC().Format(time.RFC3339))
  99. // Try create second login source with same name should fail
  100. _, err = db.Create(CreateLoginSourceOpts{Name: source.Name})
  101. expErr := ErrLoginSourceAlreadyExist{args: errutil.Args{"name": source.Name}}
  102. assert.Equal(t, expErr, err)
  103. }
  104. func test_loginSources_Count(t *testing.T, db *loginSources) {
  105. // Create two login sources, one in database and one as source file.
  106. _, err := db.Create(CreateLoginSourceOpts{
  107. Type: LoginGitHub,
  108. Name: "GitHub",
  109. Activated: true,
  110. Default: false,
  111. Config: &GitHubConfig{
  112. APIEndpoint: "https://api.github.com",
  113. },
  114. })
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. setMockLoginSourceFilesStore(t, db, &mockLoginSourceFilesStore{
  119. MockLen: func() int {
  120. return 2
  121. },
  122. })
  123. assert.Equal(t, int64(3), db.Count())
  124. }
  125. func test_loginSources_DeleteByID(t *testing.T, db *loginSources) {
  126. t.Run("delete but in used", func(t *testing.T) {
  127. source, err := db.Create(CreateLoginSourceOpts{
  128. Type: LoginGitHub,
  129. Name: "GitHub",
  130. Activated: true,
  131. Default: false,
  132. Config: &GitHubConfig{
  133. APIEndpoint: "https://api.github.com",
  134. },
  135. })
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. // Create a user that uses this login source
  140. _, err = (&users{DB: db.DB}).Create(CreateUserOpts{
  141. Name: "alice",
  142. LoginSource: source.ID,
  143. })
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. // Delete the login source will result in error
  148. err = db.DeleteByID(source.ID)
  149. expErr := ErrLoginSourceInUse{args: errutil.Args{"id": source.ID}}
  150. assert.Equal(t, expErr, err)
  151. })
  152. setMockLoginSourceFilesStore(t, db, &mockLoginSourceFilesStore{
  153. MockGetByID: func(id int64) (*LoginSource, error) {
  154. return nil, ErrLoginSourceNotExist{args: errutil.Args{"id": id}}
  155. },
  156. })
  157. // Create a login source with name "GitHub2"
  158. source, err := db.Create(CreateLoginSourceOpts{
  159. Type: LoginGitHub,
  160. Name: "GitHub2",
  161. Activated: true,
  162. Default: false,
  163. Config: &GitHubConfig{
  164. APIEndpoint: "https://api.github.com",
  165. },
  166. })
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. // Delete a non-existent ID is noop
  171. err = db.DeleteByID(9999)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. // We should be able to get it back
  176. _, err = db.GetByID(source.ID)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. // Now delete this login source with ID
  181. err = db.DeleteByID(source.ID)
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. // We should get token not found error
  186. _, err = db.GetByID(source.ID)
  187. expErr := ErrLoginSourceNotExist{args: errutil.Args{"id": source.ID}}
  188. assert.Equal(t, expErr, err)
  189. }
  190. func test_loginSources_GetByID(t *testing.T, db *loginSources) {
  191. setMockLoginSourceFilesStore(t, db, &mockLoginSourceFilesStore{
  192. MockGetByID: func(id int64) (*LoginSource, error) {
  193. if id != 101 {
  194. return nil, ErrLoginSourceNotExist{args: errutil.Args{"id": id}}
  195. }
  196. return &LoginSource{ID: id}, nil
  197. },
  198. })
  199. expConfig := &GitHubConfig{
  200. APIEndpoint: "https://api.github.com",
  201. }
  202. // Create a login source with name "GitHub"
  203. source, err := db.Create(CreateLoginSourceOpts{
  204. Type: LoginGitHub,
  205. Name: "GitHub",
  206. Activated: true,
  207. Default: false,
  208. Config: expConfig,
  209. })
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. // Get the one in the database and test the read/write hooks
  214. source, err = db.GetByID(source.ID)
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. assert.Equal(t, expConfig, source.Config)
  219. // Get the one in source file store
  220. _, err = db.GetByID(101)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. }
  225. func test_loginSources_List(t *testing.T, db *loginSources) {
  226. setMockLoginSourceFilesStore(t, db, &mockLoginSourceFilesStore{
  227. MockList: func(opts ListLoginSourceOpts) []*LoginSource {
  228. if opts.OnlyActivated {
  229. return []*LoginSource{
  230. {ID: 1},
  231. }
  232. }
  233. return []*LoginSource{
  234. {ID: 1},
  235. {ID: 2},
  236. }
  237. },
  238. })
  239. // Create two login sources in database, one activated and the other one not
  240. _, err := db.Create(CreateLoginSourceOpts{
  241. Type: LoginPAM,
  242. Name: "PAM",
  243. Config: &PAMConfig{
  244. ServiceName: "PAM",
  245. },
  246. })
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. _, err = db.Create(CreateLoginSourceOpts{
  251. Type: LoginGitHub,
  252. Name: "GitHub",
  253. Activated: true,
  254. Config: &GitHubConfig{
  255. APIEndpoint: "https://api.github.com",
  256. },
  257. })
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. // List all login sources
  262. sources, err := db.List(ListLoginSourceOpts{})
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. assert.Equal(t, 4, len(sources), "number of sources")
  267. // Only list activated login sources
  268. sources, err = db.List(ListLoginSourceOpts{OnlyActivated: true})
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. assert.Equal(t, 2, len(sources), "number of sources")
  273. }
  274. func test_loginSources_ResetNonDefault(t *testing.T, db *loginSources) {
  275. setMockLoginSourceFilesStore(t, db, &mockLoginSourceFilesStore{
  276. MockList: func(opts ListLoginSourceOpts) []*LoginSource {
  277. return []*LoginSource{
  278. {
  279. File: &mockLoginSourceFileStore{
  280. MockSetGeneral: func(name, value string) {
  281. assert.Equal(t, "is_default", name)
  282. assert.Equal(t, "false", value)
  283. },
  284. MockSave: func() error {
  285. return nil
  286. },
  287. },
  288. },
  289. }
  290. },
  291. MockUpdate: func(source *LoginSource) {},
  292. })
  293. // Create two login sources both have default on
  294. source1, err := db.Create(CreateLoginSourceOpts{
  295. Type: LoginPAM,
  296. Name: "PAM",
  297. Default: true,
  298. Config: &PAMConfig{
  299. ServiceName: "PAM",
  300. },
  301. })
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. source2, err := db.Create(CreateLoginSourceOpts{
  306. Type: LoginGitHub,
  307. Name: "GitHub",
  308. Activated: true,
  309. Default: true,
  310. Config: &GitHubConfig{
  311. APIEndpoint: "https://api.github.com",
  312. },
  313. })
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. // Set source 1 as default
  318. err = db.ResetNonDefault(source1)
  319. if err != nil {
  320. t.Fatal(err)
  321. }
  322. // Verify the default state
  323. source1, err = db.GetByID(source1.ID)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. assert.True(t, source1.IsDefault)
  328. source2, err = db.GetByID(source2.ID)
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. assert.False(t, source2.IsDefault)
  333. }
  334. func test_loginSources_Save(t *testing.T, db *loginSources) {
  335. t.Run("save to database", func(t *testing.T) {
  336. // Create a login source with name "GitHub"
  337. source, err := db.Create(CreateLoginSourceOpts{
  338. Type: LoginGitHub,
  339. Name: "GitHub",
  340. Activated: true,
  341. Default: false,
  342. Config: &GitHubConfig{
  343. APIEndpoint: "https://api.github.com",
  344. },
  345. })
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. source.IsActived = false
  350. source.Config = &GitHubConfig{
  351. APIEndpoint: "https://api2.github.com",
  352. }
  353. err = db.Save(source)
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. source, err = db.GetByID(source.ID)
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. assert.False(t, source.IsActived)
  362. assert.Equal(t, "https://api2.github.com", source.GitHub().APIEndpoint)
  363. })
  364. t.Run("save to file", func(t *testing.T) {
  365. calledSave := false
  366. source := &LoginSource{
  367. File: &mockLoginSourceFileStore{
  368. MockSetGeneral: func(name, value string) {},
  369. MockSetConfig: func(cfg interface{}) error { return nil },
  370. MockSave: func() error {
  371. calledSave = true
  372. return nil
  373. },
  374. },
  375. }
  376. err := db.Save(source)
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. assert.True(t, calledSave)
  381. })
  382. }