login_sources_test.go 8.7 KB

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