gitea源码

admin_auth_ldap_test.go 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "testing"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/modules/test"
  9. "code.gitea.io/gitea/services/auth/source/ldap"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/urfave/cli/v3"
  12. )
  13. func TestAddLdapBindDn(t *testing.T) {
  14. // Mock cli functions to do not exit on error
  15. defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
  16. // Test cases
  17. cases := []struct {
  18. args []string
  19. source *auth.Source
  20. errMsg string
  21. }{
  22. // case 0
  23. {
  24. args: []string{
  25. "ldap-test",
  26. "--name", "ldap (via Bind DN) source full",
  27. "--not-active",
  28. "--security-protocol", "ldaps",
  29. "--skip-tls-verify",
  30. "--host", "ldap-bind-server full",
  31. "--port", "9876",
  32. "--user-search-base", "ou=Users,dc=full-domain-bind,dc=org",
  33. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=full-domain-bind,dc=org)",
  34. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
  35. "--restricted-filter", "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
  36. "--username-attribute", "uid-bind full",
  37. "--firstname-attribute", "givenName-bind full",
  38. "--surname-attribute", "sn-bind full",
  39. "--email-attribute", "mail-bind full",
  40. "--public-ssh-key-attribute", "publickey-bind full",
  41. "--avatar-attribute", "avatar-bind full",
  42. "--bind-dn", "cn=readonly,dc=full-domain-bind,dc=org",
  43. "--bind-password", "secret-bind-full",
  44. "--attributes-in-bind",
  45. "--synchronize-users",
  46. "--page-size", "99",
  47. "--enable-groups",
  48. "--group-search-base-dn", "ou=group,dc=full-domain-bind,dc=org",
  49. "--group-member-attribute", "memberUid",
  50. "--group-user-attribute", "uid",
  51. "--group-filter", "(|(cn=gitea_users)(cn=admins))",
  52. "--group-team-map", `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
  53. "--group-team-map-removal",
  54. },
  55. source: &auth.Source{
  56. Type: auth.LDAP,
  57. Name: "ldap (via Bind DN) source full",
  58. IsActive: false,
  59. IsSyncEnabled: true,
  60. Cfg: &ldap.Source{
  61. Name: "ldap (via Bind DN) source full",
  62. Host: "ldap-bind-server full",
  63. Port: 9876,
  64. SecurityProtocol: ldap.SecurityProtocol(1),
  65. SkipVerify: true,
  66. BindDN: "cn=readonly,dc=full-domain-bind,dc=org",
  67. BindPassword: "secret-bind-full",
  68. UserBase: "ou=Users,dc=full-domain-bind,dc=org",
  69. AttributeUsername: "uid-bind full",
  70. AttributeName: "givenName-bind full",
  71. AttributeSurname: "sn-bind full",
  72. AttributeMail: "mail-bind full",
  73. AttributesInBind: true,
  74. AttributeSSHPublicKey: "publickey-bind full",
  75. AttributeAvatar: "avatar-bind full",
  76. SearchPageSize: 99,
  77. Filter: "(memberOf=cn=user-group,ou=example,dc=full-domain-bind,dc=org)",
  78. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
  79. RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
  80. Enabled: true,
  81. GroupsEnabled: true,
  82. GroupDN: "ou=group,dc=full-domain-bind,dc=org",
  83. GroupMemberUID: "memberUid",
  84. UserUID: "uid",
  85. GroupFilter: "(|(cn=gitea_users)(cn=admins))",
  86. GroupTeamMap: `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
  87. GroupTeamMapRemoval: true,
  88. },
  89. },
  90. },
  91. // case 1
  92. {
  93. args: []string{
  94. "ldap-test",
  95. "--name", "ldap (via Bind DN) source min",
  96. "--security-protocol", "unencrypted",
  97. "--host", "ldap-bind-server min",
  98. "--port", "1234",
  99. "--user-search-base", "ou=Users,dc=min-domain-bind,dc=org",
  100. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=min-domain-bind,dc=org)",
  101. "--email-attribute", "mail-bind min",
  102. },
  103. source: &auth.Source{
  104. Type: auth.LDAP,
  105. Name: "ldap (via Bind DN) source min",
  106. IsActive: true,
  107. Cfg: &ldap.Source{
  108. Name: "ldap (via Bind DN) source min",
  109. Host: "ldap-bind-server min",
  110. Port: 1234,
  111. SecurityProtocol: ldap.SecurityProtocol(0),
  112. UserBase: "ou=Users,dc=min-domain-bind,dc=org",
  113. AttributeMail: "mail-bind min",
  114. Filter: "(memberOf=cn=user-group,ou=example,dc=min-domain-bind,dc=org)",
  115. Enabled: true,
  116. },
  117. },
  118. },
  119. // case 2
  120. {
  121. args: []string{
  122. "ldap-test",
  123. "--name", "ldap (via Bind DN) source",
  124. "--security-protocol", "zzzzz",
  125. "--host", "ldap-server",
  126. "--port", "1234",
  127. "--user-search-base", "ou=Users,dc=domain,dc=org",
  128. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  129. "--email-attribute", "mail",
  130. },
  131. errMsg: "unknown security protocol name: zzzzz",
  132. },
  133. // case 3
  134. {
  135. args: []string{
  136. "ldap-test",
  137. "--security-protocol", "unencrypted",
  138. "--host", "ldap-server",
  139. "--port", "1234",
  140. "--user-search-base", "ou=Users,dc=domain,dc=org",
  141. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  142. "--email-attribute", "mail",
  143. },
  144. errMsg: "name is not set",
  145. },
  146. // case 4
  147. {
  148. args: []string{
  149. "ldap-test",
  150. "--name", "ldap (via Bind DN) source",
  151. "--host", "ldap-server",
  152. "--port", "1234",
  153. "--user-search-base", "ou=Users,dc=domain,dc=org",
  154. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  155. "--email-attribute", "mail",
  156. },
  157. errMsg: "security-protocol is not set",
  158. },
  159. // case 5
  160. {
  161. args: []string{
  162. "ldap-test",
  163. "--name", "ldap (via Bind DN) source",
  164. "--security-protocol", "unencrypted",
  165. "--port", "1234",
  166. "--user-search-base", "ou=Users,dc=domain,dc=org",
  167. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  168. "--email-attribute", "mail",
  169. },
  170. errMsg: "host is not set",
  171. },
  172. // case 6
  173. {
  174. args: []string{
  175. "ldap-test",
  176. "--name", "ldap (via Bind DN) source",
  177. "--security-protocol", "unencrypted",
  178. "--host", "ldap-server",
  179. "--user-search-base", "ou=Users,dc=domain,dc=org",
  180. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  181. "--email-attribute", "mail",
  182. },
  183. errMsg: "port is not set",
  184. },
  185. // case 7
  186. {
  187. args: []string{
  188. "ldap-test",
  189. "--name", "ldap (via Bind DN) source",
  190. "--security-protocol", "unencrypted",
  191. "--host", "ldap-server",
  192. "--port", "1234",
  193. "--user-search-base", "ou=Users,dc=domain,dc=org",
  194. "--email-attribute", "mail",
  195. },
  196. errMsg: "user-filter is not set",
  197. },
  198. // case 8
  199. {
  200. args: []string{
  201. "ldap-test",
  202. "--name", "ldap (via Bind DN) source",
  203. "--security-protocol", "unencrypted",
  204. "--host", "ldap-server",
  205. "--port", "1234",
  206. "--user-search-base", "ou=Users,dc=domain,dc=org",
  207. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  208. },
  209. errMsg: "email-attribute is not set",
  210. },
  211. }
  212. for n, c := range cases {
  213. // Mock functions.
  214. var createdAuthSource *auth.Source
  215. service := &authService{
  216. initDB: func(context.Context) error {
  217. return nil
  218. },
  219. createAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  220. createdAuthSource = authSource
  221. return nil
  222. },
  223. updateAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  224. assert.FailNow(t, "updateAuthSource called", "case %d: should not call updateAuthSource", n)
  225. return nil
  226. },
  227. getAuthSourceByID: func(ctx context.Context, id int64) (*auth.Source, error) {
  228. assert.FailNow(t, "getAuthSourceByID called", "case %d: should not call getAuthSourceByID", n)
  229. return nil, nil
  230. },
  231. }
  232. // Create a copy of command to test
  233. app := cli.Command{
  234. Flags: microcmdAuthAddLdapBindDn().Flags,
  235. Action: service.addLdapBindDn,
  236. }
  237. // Run it
  238. err := app.Run(t.Context(), c.args)
  239. if c.errMsg != "" {
  240. assert.EqualError(t, err, c.errMsg, "case %d: error should match", n)
  241. } else {
  242. assert.NoError(t, err, "case %d: should have no errors", n)
  243. assert.Equal(t, c.source, createdAuthSource, "case %d: wrong authSource", n)
  244. }
  245. }
  246. }
  247. func TestAddLdapSimpleAuth(t *testing.T) {
  248. // Mock cli functions to do not exit on error
  249. defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
  250. // Test cases
  251. cases := []struct {
  252. args []string
  253. authSource *auth.Source
  254. errMsg string
  255. }{
  256. // case 0
  257. {
  258. args: []string{
  259. "ldap-test",
  260. "--name", "ldap (simple auth) source full",
  261. "--not-active",
  262. "--security-protocol", "starttls",
  263. "--skip-tls-verify",
  264. "--host", "ldap-simple-server full",
  265. "--port", "987",
  266. "--user-search-base", "ou=Users,dc=full-domain-simple,dc=org",
  267. "--user-filter", "(&(objectClass=posixAccount)(full-simple-cn=%s))",
  268. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=full-domain-simple,dc=org)",
  269. "--restricted-filter", "(memberOf=cn=restricted-group,ou=example,dc=full-domain-simple,dc=org)",
  270. "--username-attribute", "uid-simple full",
  271. "--firstname-attribute", "givenName-simple full",
  272. "--surname-attribute", "sn-simple full",
  273. "--email-attribute", "mail-simple full",
  274. "--public-ssh-key-attribute", "publickey-simple full",
  275. "--avatar-attribute", "avatar-simple full",
  276. "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
  277. },
  278. authSource: &auth.Source{
  279. Type: auth.DLDAP,
  280. Name: "ldap (simple auth) source full",
  281. IsActive: false,
  282. Cfg: &ldap.Source{
  283. Name: "ldap (simple auth) source full",
  284. Host: "ldap-simple-server full",
  285. Port: 987,
  286. SecurityProtocol: ldap.SecurityProtocol(2),
  287. SkipVerify: true,
  288. UserDN: "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
  289. UserBase: "ou=Users,dc=full-domain-simple,dc=org",
  290. AttributeUsername: "uid-simple full",
  291. AttributeName: "givenName-simple full",
  292. AttributeSurname: "sn-simple full",
  293. AttributeMail: "mail-simple full",
  294. AttributeSSHPublicKey: "publickey-simple full",
  295. AttributeAvatar: "avatar-simple full",
  296. Filter: "(&(objectClass=posixAccount)(full-simple-cn=%s))",
  297. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-simple,dc=org)",
  298. RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-simple,dc=org)",
  299. Enabled: true,
  300. },
  301. },
  302. },
  303. // case 1
  304. {
  305. args: []string{
  306. "ldap-test",
  307. "--name", "ldap (simple auth) source min",
  308. "--security-protocol", "unencrypted",
  309. "--host", "ldap-simple-server min",
  310. "--port", "123",
  311. "--user-filter", "(&(objectClass=posixAccount)(min-simple-cn=%s))",
  312. "--email-attribute", "mail-simple min",
  313. "--user-dn", "cn=%s,ou=Users,dc=min-domain-simple,dc=org",
  314. },
  315. authSource: &auth.Source{
  316. Type: auth.DLDAP,
  317. Name: "ldap (simple auth) source min",
  318. IsActive: true,
  319. Cfg: &ldap.Source{
  320. Name: "ldap (simple auth) source min",
  321. Host: "ldap-simple-server min",
  322. Port: 123,
  323. SecurityProtocol: ldap.SecurityProtocol(0),
  324. UserDN: "cn=%s,ou=Users,dc=min-domain-simple,dc=org",
  325. AttributeMail: "mail-simple min",
  326. Filter: "(&(objectClass=posixAccount)(min-simple-cn=%s))",
  327. Enabled: true,
  328. },
  329. },
  330. },
  331. // case 2
  332. {
  333. args: []string{
  334. "ldap-test",
  335. "--name", "ldap (simple auth) source",
  336. "--security-protocol", "zzzzz",
  337. "--host", "ldap-server",
  338. "--port", "1234",
  339. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  340. "--email-attribute", "mail",
  341. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  342. },
  343. errMsg: "unknown security protocol name: zzzzz",
  344. },
  345. // case 3
  346. {
  347. args: []string{
  348. "ldap-test",
  349. "--security-protocol", "unencrypted",
  350. "--host", "ldap-server",
  351. "--port", "123",
  352. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  353. "--email-attribute", "mail",
  354. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  355. },
  356. errMsg: "name is not set",
  357. },
  358. // case 4
  359. {
  360. args: []string{
  361. "ldap-test",
  362. "--name", "ldap (simple auth) source",
  363. "--host", "ldap-server",
  364. "--port", "123",
  365. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  366. "--email-attribute", "mail",
  367. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  368. },
  369. errMsg: "security-protocol is not set",
  370. },
  371. // case 5
  372. {
  373. args: []string{
  374. "ldap-test",
  375. "--name", "ldap (simple auth) source",
  376. "--security-protocol", "unencrypted",
  377. "--port", "123",
  378. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  379. "--email-attribute", "mail",
  380. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  381. },
  382. errMsg: "host is not set",
  383. },
  384. // case 6
  385. {
  386. args: []string{
  387. "ldap-test",
  388. "--name", "ldap (simple auth) source",
  389. "--security-protocol", "unencrypted",
  390. "--host", "ldap-server",
  391. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  392. "--email-attribute", "mail",
  393. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  394. },
  395. errMsg: "port is not set",
  396. },
  397. // case 7
  398. {
  399. args: []string{
  400. "ldap-test",
  401. "--name", "ldap (simple auth) source",
  402. "--security-protocol", "unencrypted",
  403. "--host", "ldap-server",
  404. "--port", "123",
  405. "--email-attribute", "mail",
  406. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  407. },
  408. errMsg: "user-filter is not set",
  409. },
  410. // case 8
  411. {
  412. args: []string{
  413. "ldap-test",
  414. "--name", "ldap (simple auth) source",
  415. "--security-protocol", "unencrypted",
  416. "--host", "ldap-server",
  417. "--port", "123",
  418. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  419. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  420. },
  421. errMsg: "email-attribute is not set",
  422. },
  423. // case 9
  424. {
  425. args: []string{
  426. "ldap-test",
  427. "--name", "ldap (simple auth) source",
  428. "--security-protocol", "unencrypted",
  429. "--host", "ldap-server",
  430. "--port", "123",
  431. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  432. "--email-attribute", "mail",
  433. },
  434. errMsg: "user-dn is not set",
  435. },
  436. }
  437. for n, c := range cases {
  438. // Mock functions.
  439. var createdAuthSource *auth.Source
  440. service := &authService{
  441. initDB: func(context.Context) error {
  442. return nil
  443. },
  444. createAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  445. createdAuthSource = authSource
  446. return nil
  447. },
  448. updateAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  449. assert.FailNow(t, "updateAuthSource called", "case %d: should not call updateAuthSource", n)
  450. return nil
  451. },
  452. getAuthSourceByID: func(ctx context.Context, id int64) (*auth.Source, error) {
  453. assert.FailNow(t, "getAuthSourceById called", "case %d: should not call getAuthSourceByID", n)
  454. return nil, nil
  455. },
  456. }
  457. // Create a copy of command to test
  458. app := &cli.Command{
  459. Flags: microcmdAuthAddLdapSimpleAuth().Flags,
  460. Action: service.addLdapSimpleAuth,
  461. }
  462. // Run it
  463. err := app.Run(t.Context(), c.args)
  464. if c.errMsg != "" {
  465. assert.EqualError(t, err, c.errMsg, "case %d: error should match", n)
  466. } else {
  467. assert.NoError(t, err, "case %d: should have no errors", n)
  468. assert.Equal(t, c.authSource, createdAuthSource, "case %d: wrong authSource", n)
  469. }
  470. }
  471. }
  472. func TestUpdateLdapBindDn(t *testing.T) {
  473. // Mock cli functions to do not exit on error
  474. defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
  475. // Test cases
  476. cases := []struct {
  477. args []string
  478. id int64
  479. existingAuthSource *auth.Source
  480. authSource *auth.Source
  481. errMsg string
  482. }{
  483. // case 0
  484. {
  485. args: []string{
  486. "ldap-test",
  487. "--id", "23",
  488. "--name", "ldap (via Bind DN) source full",
  489. "--not-active",
  490. "--security-protocol", "LDAPS",
  491. "--skip-tls-verify",
  492. "--host", "ldap-bind-server full",
  493. "--port", "9876",
  494. "--user-search-base", "ou=Users,dc=full-domain-bind,dc=org",
  495. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=full-domain-bind,dc=org)",
  496. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
  497. "--restricted-filter", "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
  498. "--username-attribute", "uid-bind full",
  499. "--firstname-attribute", "givenName-bind full",
  500. "--surname-attribute", "sn-bind full",
  501. "--email-attribute", "mail-bind full",
  502. "--public-ssh-key-attribute", "publickey-bind full",
  503. "--avatar-attribute", "avatar-bind full",
  504. "--bind-dn", "cn=readonly,dc=full-domain-bind,dc=org",
  505. "--bind-password", "secret-bind-full",
  506. "--synchronize-users",
  507. "--page-size", "99",
  508. "--enable-groups",
  509. "--group-search-base-dn", "ou=group,dc=full-domain-bind,dc=org",
  510. "--group-member-attribute", "memberUid",
  511. "--group-user-attribute", "uid",
  512. "--group-filter", "(|(cn=gitea_users)(cn=admins))",
  513. "--group-team-map", `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
  514. "--group-team-map-removal",
  515. },
  516. id: 23,
  517. existingAuthSource: &auth.Source{
  518. Type: auth.LDAP,
  519. IsActive: true,
  520. Cfg: &ldap.Source{
  521. Enabled: true,
  522. },
  523. },
  524. authSource: &auth.Source{
  525. Type: auth.LDAP,
  526. Name: "ldap (via Bind DN) source full",
  527. IsActive: false,
  528. IsSyncEnabled: true,
  529. Cfg: &ldap.Source{
  530. Name: "ldap (via Bind DN) source full",
  531. Host: "ldap-bind-server full",
  532. Port: 9876,
  533. SecurityProtocol: ldap.SecurityProtocol(1),
  534. SkipVerify: true,
  535. BindDN: "cn=readonly,dc=full-domain-bind,dc=org",
  536. BindPassword: "secret-bind-full",
  537. UserBase: "ou=Users,dc=full-domain-bind,dc=org",
  538. AttributeUsername: "uid-bind full",
  539. AttributeName: "givenName-bind full",
  540. AttributeSurname: "sn-bind full",
  541. AttributeMail: "mail-bind full",
  542. AttributesInBind: false,
  543. AttributeSSHPublicKey: "publickey-bind full",
  544. AttributeAvatar: "avatar-bind full",
  545. SearchPageSize: 99,
  546. Filter: "(memberOf=cn=user-group,ou=example,dc=full-domain-bind,dc=org)",
  547. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
  548. RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
  549. Enabled: true,
  550. GroupsEnabled: true,
  551. GroupDN: "ou=group,dc=full-domain-bind,dc=org",
  552. GroupMemberUID: "memberUid",
  553. UserUID: "uid",
  554. GroupFilter: "(|(cn=gitea_users)(cn=admins))",
  555. GroupTeamMap: `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
  556. GroupTeamMapRemoval: true,
  557. },
  558. },
  559. },
  560. // case 1
  561. {
  562. args: []string{
  563. "ldap-test",
  564. "--id", "1",
  565. },
  566. authSource: &auth.Source{
  567. Type: auth.LDAP,
  568. Cfg: &ldap.Source{},
  569. },
  570. },
  571. // case 2
  572. {
  573. args: []string{
  574. "ldap-test",
  575. "--id", "1",
  576. "--name", "ldap (via Bind DN) source",
  577. },
  578. authSource: &auth.Source{
  579. Type: auth.LDAP,
  580. Name: "ldap (via Bind DN) source",
  581. Cfg: &ldap.Source{
  582. Name: "ldap (via Bind DN) source",
  583. },
  584. },
  585. },
  586. // case 3
  587. {
  588. args: []string{
  589. "ldap-test",
  590. "--id", "1",
  591. "--not-active",
  592. },
  593. existingAuthSource: &auth.Source{
  594. Type: auth.LDAP,
  595. IsActive: true,
  596. Cfg: &ldap.Source{},
  597. },
  598. authSource: &auth.Source{
  599. Type: auth.LDAP,
  600. IsActive: false,
  601. Cfg: &ldap.Source{},
  602. },
  603. },
  604. // case 4
  605. {
  606. args: []string{
  607. "ldap-test",
  608. "--id", "1",
  609. "--security-protocol", "LDAPS",
  610. },
  611. authSource: &auth.Source{
  612. Type: auth.LDAP,
  613. Cfg: &ldap.Source{
  614. SecurityProtocol: ldap.SecurityProtocol(1),
  615. },
  616. },
  617. },
  618. // case 5
  619. {
  620. args: []string{
  621. "ldap-test",
  622. "--id", "1",
  623. "--skip-tls-verify",
  624. },
  625. authSource: &auth.Source{
  626. Type: auth.LDAP,
  627. Cfg: &ldap.Source{
  628. SkipVerify: true,
  629. },
  630. },
  631. },
  632. // case 6
  633. {
  634. args: []string{
  635. "ldap-test",
  636. "--id", "1",
  637. "--host", "ldap-server",
  638. },
  639. authSource: &auth.Source{
  640. Type: auth.LDAP,
  641. Cfg: &ldap.Source{
  642. Host: "ldap-server",
  643. },
  644. },
  645. },
  646. // case 7
  647. {
  648. args: []string{
  649. "ldap-test",
  650. "--id", "1",
  651. "--port", "389",
  652. },
  653. authSource: &auth.Source{
  654. Type: auth.LDAP,
  655. Cfg: &ldap.Source{
  656. Port: 389,
  657. },
  658. },
  659. },
  660. // case 8
  661. {
  662. args: []string{
  663. "ldap-test",
  664. "--id", "1",
  665. "--user-search-base", "ou=Users,dc=domain,dc=org",
  666. },
  667. authSource: &auth.Source{
  668. Type: auth.LDAP,
  669. Cfg: &ldap.Source{
  670. UserBase: "ou=Users,dc=domain,dc=org",
  671. },
  672. },
  673. },
  674. // case 9
  675. {
  676. args: []string{
  677. "ldap-test",
  678. "--id", "1",
  679. "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  680. },
  681. authSource: &auth.Source{
  682. Type: auth.LDAP,
  683. Cfg: &ldap.Source{
  684. Filter: "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
  685. },
  686. },
  687. },
  688. // case 10
  689. {
  690. args: []string{
  691. "ldap-test",
  692. "--id", "1",
  693. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
  694. },
  695. authSource: &auth.Source{
  696. Type: auth.LDAP,
  697. Cfg: &ldap.Source{
  698. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
  699. },
  700. },
  701. },
  702. // case 11
  703. {
  704. args: []string{
  705. "ldap-test",
  706. "--id", "1",
  707. "--username-attribute", "uid",
  708. },
  709. authSource: &auth.Source{
  710. Type: auth.LDAP,
  711. Cfg: &ldap.Source{
  712. AttributeUsername: "uid",
  713. },
  714. },
  715. },
  716. // case 12
  717. {
  718. args: []string{
  719. "ldap-test",
  720. "--id", "1",
  721. "--firstname-attribute", "givenName",
  722. },
  723. authSource: &auth.Source{
  724. Type: auth.LDAP,
  725. Cfg: &ldap.Source{
  726. AttributeName: "givenName",
  727. },
  728. },
  729. },
  730. // case 13
  731. {
  732. args: []string{
  733. "ldap-test",
  734. "--id", "1",
  735. "--surname-attribute", "sn",
  736. },
  737. authSource: &auth.Source{
  738. Type: auth.LDAP,
  739. Cfg: &ldap.Source{
  740. AttributeSurname: "sn",
  741. },
  742. },
  743. },
  744. // case 14
  745. {
  746. args: []string{
  747. "ldap-test",
  748. "--id", "1",
  749. "--email-attribute", "mail",
  750. },
  751. authSource: &auth.Source{
  752. Type: auth.LDAP,
  753. Cfg: &ldap.Source{
  754. AttributeMail: "mail",
  755. },
  756. },
  757. },
  758. // case 15
  759. {
  760. args: []string{
  761. "ldap-test",
  762. "--id", "1",
  763. "--attributes-in-bind",
  764. },
  765. authSource: &auth.Source{
  766. Type: auth.LDAP,
  767. Cfg: &ldap.Source{
  768. AttributesInBind: true,
  769. },
  770. },
  771. },
  772. // case 16
  773. {
  774. args: []string{
  775. "ldap-test",
  776. "--id", "1",
  777. "--public-ssh-key-attribute", "publickey",
  778. },
  779. authSource: &auth.Source{
  780. Type: auth.LDAP,
  781. Cfg: &ldap.Source{
  782. AttributeSSHPublicKey: "publickey",
  783. },
  784. },
  785. },
  786. // case 17
  787. {
  788. args: []string{
  789. "ldap-test",
  790. "--id", "1",
  791. "--bind-dn", "cn=readonly,dc=domain,dc=org",
  792. },
  793. authSource: &auth.Source{
  794. Type: auth.LDAP,
  795. Cfg: &ldap.Source{
  796. BindDN: "cn=readonly,dc=domain,dc=org",
  797. },
  798. },
  799. },
  800. // case 18
  801. {
  802. args: []string{
  803. "ldap-test",
  804. "--id", "1",
  805. "--bind-password", "secret",
  806. },
  807. authSource: &auth.Source{
  808. Type: auth.LDAP,
  809. Cfg: &ldap.Source{
  810. BindPassword: "secret",
  811. },
  812. },
  813. },
  814. // case 19
  815. {
  816. args: []string{
  817. "ldap-test",
  818. "--id", "1",
  819. "--synchronize-users",
  820. },
  821. authSource: &auth.Source{
  822. Type: auth.LDAP,
  823. IsSyncEnabled: true,
  824. Cfg: &ldap.Source{},
  825. },
  826. },
  827. // case 20
  828. {
  829. args: []string{
  830. "ldap-test",
  831. "--id", "1",
  832. "--page-size", "12",
  833. },
  834. authSource: &auth.Source{
  835. Type: auth.LDAP,
  836. Cfg: &ldap.Source{
  837. SearchPageSize: 12,
  838. },
  839. },
  840. },
  841. // case 21
  842. {
  843. args: []string{
  844. "ldap-test",
  845. "--id", "1",
  846. "--security-protocol", "xxxxx",
  847. },
  848. errMsg: "unknown security protocol name: xxxxx",
  849. },
  850. // case 22
  851. {
  852. args: []string{
  853. "ldap-test",
  854. },
  855. errMsg: "id is not set",
  856. },
  857. // case 23
  858. {
  859. args: []string{
  860. "ldap-test",
  861. "--id", "1",
  862. },
  863. existingAuthSource: &auth.Source{
  864. Type: auth.OAuth2,
  865. Cfg: &ldap.Source{},
  866. },
  867. errMsg: "invalid authentication type. expected: LDAP (via BindDN), actual: OAuth2",
  868. },
  869. // case 24
  870. {
  871. args: []string{
  872. "ldap-test",
  873. "--id", "24",
  874. "--name", "ldap (via Bind DN) flip 'active' and 'user sync' attributes",
  875. "--active",
  876. "--disable-synchronize-users",
  877. },
  878. id: 24,
  879. existingAuthSource: &auth.Source{
  880. Type: auth.LDAP,
  881. IsActive: false,
  882. IsSyncEnabled: true,
  883. Cfg: &ldap.Source{
  884. Name: "ldap (via Bind DN) flip 'active' and 'user sync' attributes",
  885. Enabled: true,
  886. },
  887. },
  888. authSource: &auth.Source{
  889. Type: auth.LDAP,
  890. Name: "ldap (via Bind DN) flip 'active' and 'user sync' attributes",
  891. IsActive: true,
  892. IsSyncEnabled: false,
  893. Cfg: &ldap.Source{
  894. Name: "ldap (via Bind DN) flip 'active' and 'user sync' attributes",
  895. Enabled: true,
  896. },
  897. },
  898. },
  899. }
  900. for n, c := range cases {
  901. // Mock functions.
  902. var updatedAuthSource *auth.Source
  903. service := &authService{
  904. initDB: func(context.Context) error {
  905. return nil
  906. },
  907. createAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  908. assert.FailNow(t, "createAuthSource called", "case %d: should not call createAuthSource", n)
  909. return nil
  910. },
  911. updateAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  912. updatedAuthSource = authSource
  913. return nil
  914. },
  915. getAuthSourceByID: func(ctx context.Context, id int64) (*auth.Source, error) {
  916. if c.id != 0 {
  917. assert.Equal(t, c.id, id, "case %d: wrong id", n)
  918. }
  919. if c.existingAuthSource != nil {
  920. return c.existingAuthSource, nil
  921. }
  922. return &auth.Source{
  923. Type: auth.LDAP,
  924. Cfg: &ldap.Source{},
  925. }, nil
  926. },
  927. }
  928. // Create a copy of command to test
  929. app := cli.Command{
  930. Flags: microcmdAuthUpdateLdapBindDn().Flags,
  931. Action: service.updateLdapBindDn,
  932. }
  933. // Run it
  934. err := app.Run(t.Context(), c.args)
  935. if c.errMsg != "" {
  936. assert.EqualError(t, err, c.errMsg, "case %d: error should match", n)
  937. } else {
  938. assert.NoError(t, err, "case %d: should have no errors", n)
  939. assert.Equal(t, c.authSource, updatedAuthSource, "case %d: wrong authSource", n)
  940. }
  941. }
  942. }
  943. func TestUpdateLdapSimpleAuth(t *testing.T) {
  944. // Mock cli functions to do not exit on error
  945. defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
  946. // Test cases
  947. cases := []struct {
  948. args []string
  949. id int64
  950. existingAuthSource *auth.Source
  951. authSource *auth.Source
  952. errMsg string
  953. }{
  954. // case 0
  955. {
  956. args: []string{
  957. "ldap-test",
  958. "--id", "7",
  959. "--name", "ldap (simple auth) source full",
  960. "--not-active",
  961. "--security-protocol", "starttls",
  962. "--skip-tls-verify",
  963. "--host", "ldap-simple-server full",
  964. "--port", "987",
  965. "--user-search-base", "ou=Users,dc=full-domain-simple,dc=org",
  966. "--user-filter", "(&(objectClass=posixAccount)(full-simple-cn=%s))",
  967. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=full-domain-simple,dc=org)",
  968. "--restricted-filter", "(memberOf=cn=restricted-group,ou=example,dc=full-domain-simple,dc=org)",
  969. "--username-attribute", "uid-simple full",
  970. "--firstname-attribute", "givenName-simple full",
  971. "--surname-attribute", "sn-simple full",
  972. "--email-attribute", "mail-simple full",
  973. "--public-ssh-key-attribute", "publickey-simple full",
  974. "--avatar-attribute", "avatar-simple full",
  975. "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
  976. },
  977. id: 7,
  978. authSource: &auth.Source{
  979. Type: auth.DLDAP,
  980. Name: "ldap (simple auth) source full",
  981. IsActive: false,
  982. Cfg: &ldap.Source{
  983. Name: "ldap (simple auth) source full",
  984. Host: "ldap-simple-server full",
  985. Port: 987,
  986. SecurityProtocol: ldap.SecurityProtocol(2),
  987. SkipVerify: true,
  988. UserDN: "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
  989. UserBase: "ou=Users,dc=full-domain-simple,dc=org",
  990. AttributeUsername: "uid-simple full",
  991. AttributeName: "givenName-simple full",
  992. AttributeSurname: "sn-simple full",
  993. AttributeMail: "mail-simple full",
  994. AttributeSSHPublicKey: "publickey-simple full",
  995. AttributeAvatar: "avatar-simple full",
  996. Filter: "(&(objectClass=posixAccount)(full-simple-cn=%s))",
  997. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-simple,dc=org)",
  998. RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-simple,dc=org)",
  999. },
  1000. },
  1001. },
  1002. // case 1
  1003. {
  1004. args: []string{
  1005. "ldap-test",
  1006. "--id", "1",
  1007. },
  1008. authSource: &auth.Source{
  1009. Type: auth.DLDAP,
  1010. Cfg: &ldap.Source{},
  1011. },
  1012. },
  1013. // case 2
  1014. {
  1015. args: []string{
  1016. "ldap-test",
  1017. "--id", "1",
  1018. "--name", "ldap (simple auth) source",
  1019. },
  1020. authSource: &auth.Source{
  1021. Type: auth.DLDAP,
  1022. Name: "ldap (simple auth) source",
  1023. Cfg: &ldap.Source{
  1024. Name: "ldap (simple auth) source",
  1025. },
  1026. },
  1027. },
  1028. // case 3
  1029. {
  1030. args: []string{
  1031. "ldap-test",
  1032. "--id", "1",
  1033. "--not-active",
  1034. },
  1035. existingAuthSource: &auth.Source{
  1036. Type: auth.DLDAP,
  1037. IsActive: true,
  1038. Cfg: &ldap.Source{},
  1039. },
  1040. authSource: &auth.Source{
  1041. Type: auth.DLDAP,
  1042. IsActive: false,
  1043. Cfg: &ldap.Source{},
  1044. },
  1045. },
  1046. // case 4
  1047. {
  1048. args: []string{
  1049. "ldap-test",
  1050. "--id", "1",
  1051. "--security-protocol", "starttls",
  1052. },
  1053. authSource: &auth.Source{
  1054. Type: auth.DLDAP,
  1055. Cfg: &ldap.Source{
  1056. SecurityProtocol: ldap.SecurityProtocol(2),
  1057. },
  1058. },
  1059. },
  1060. // case 5
  1061. {
  1062. args: []string{
  1063. "ldap-test",
  1064. "--id", "1",
  1065. "--skip-tls-verify",
  1066. },
  1067. authSource: &auth.Source{
  1068. Type: auth.DLDAP,
  1069. Cfg: &ldap.Source{
  1070. SkipVerify: true,
  1071. },
  1072. },
  1073. },
  1074. // case 6
  1075. {
  1076. args: []string{
  1077. "ldap-test",
  1078. "--id", "1",
  1079. "--host", "ldap-server",
  1080. },
  1081. authSource: &auth.Source{
  1082. Type: auth.DLDAP,
  1083. Cfg: &ldap.Source{
  1084. Host: "ldap-server",
  1085. },
  1086. },
  1087. },
  1088. // case 7
  1089. {
  1090. args: []string{
  1091. "ldap-test",
  1092. "--id", "1",
  1093. "--port", "987",
  1094. },
  1095. authSource: &auth.Source{
  1096. Type: auth.DLDAP,
  1097. Cfg: &ldap.Source{
  1098. Port: 987,
  1099. },
  1100. },
  1101. },
  1102. // case 8
  1103. {
  1104. args: []string{
  1105. "ldap-test",
  1106. "--id", "1",
  1107. "--user-search-base", "ou=Users,dc=domain,dc=org",
  1108. },
  1109. authSource: &auth.Source{
  1110. Type: auth.DLDAP,
  1111. Cfg: &ldap.Source{
  1112. UserBase: "ou=Users,dc=domain,dc=org",
  1113. },
  1114. },
  1115. },
  1116. // case 9
  1117. {
  1118. args: []string{
  1119. "ldap-test",
  1120. "--id", "1",
  1121. "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
  1122. },
  1123. authSource: &auth.Source{
  1124. Type: auth.DLDAP,
  1125. Cfg: &ldap.Source{
  1126. Filter: "(&(objectClass=posixAccount)(cn=%s))",
  1127. },
  1128. },
  1129. },
  1130. // case 10
  1131. {
  1132. args: []string{
  1133. "ldap-test",
  1134. "--id", "1",
  1135. "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
  1136. },
  1137. authSource: &auth.Source{
  1138. Type: auth.DLDAP,
  1139. Cfg: &ldap.Source{
  1140. AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
  1141. },
  1142. },
  1143. },
  1144. // case 11
  1145. {
  1146. args: []string{
  1147. "ldap-test",
  1148. "--id", "1",
  1149. "--username-attribute", "uid",
  1150. },
  1151. authSource: &auth.Source{
  1152. Type: auth.DLDAP,
  1153. Cfg: &ldap.Source{
  1154. AttributeUsername: "uid",
  1155. },
  1156. },
  1157. },
  1158. // case 12
  1159. {
  1160. args: []string{
  1161. "ldap-test",
  1162. "--id", "1",
  1163. "--firstname-attribute", "givenName",
  1164. },
  1165. authSource: &auth.Source{
  1166. Type: auth.DLDAP,
  1167. Cfg: &ldap.Source{
  1168. AttributeName: "givenName",
  1169. },
  1170. },
  1171. },
  1172. // case 13
  1173. {
  1174. args: []string{
  1175. "ldap-test",
  1176. "--id", "1",
  1177. "--surname-attribute", "sn",
  1178. },
  1179. authSource: &auth.Source{
  1180. Type: auth.DLDAP,
  1181. Cfg: &ldap.Source{
  1182. AttributeSurname: "sn",
  1183. },
  1184. },
  1185. },
  1186. // case 14
  1187. {
  1188. args: []string{
  1189. "ldap-test",
  1190. "--id", "1",
  1191. "--email-attribute", "mail",
  1192. },
  1193. authSource: &auth.Source{
  1194. Type: auth.DLDAP,
  1195. Cfg: &ldap.Source{
  1196. AttributeMail: "mail",
  1197. },
  1198. },
  1199. },
  1200. // case 15
  1201. {
  1202. args: []string{
  1203. "ldap-test",
  1204. "--id", "1",
  1205. "--public-ssh-key-attribute", "publickey",
  1206. },
  1207. authSource: &auth.Source{
  1208. Type: auth.DLDAP,
  1209. Cfg: &ldap.Source{
  1210. AttributeSSHPublicKey: "publickey",
  1211. },
  1212. },
  1213. },
  1214. // case 16
  1215. {
  1216. args: []string{
  1217. "ldap-test",
  1218. "--id", "1",
  1219. "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
  1220. },
  1221. authSource: &auth.Source{
  1222. Type: auth.DLDAP,
  1223. Cfg: &ldap.Source{
  1224. UserDN: "cn=%s,ou=Users,dc=domain,dc=org",
  1225. },
  1226. },
  1227. },
  1228. // case 17
  1229. {
  1230. args: []string{
  1231. "ldap-test",
  1232. "--id", "1",
  1233. "--security-protocol", "xxxxx",
  1234. },
  1235. errMsg: "unknown security protocol name: xxxxx",
  1236. },
  1237. // case 18
  1238. {
  1239. args: []string{
  1240. "ldap-test",
  1241. },
  1242. errMsg: "id is not set",
  1243. },
  1244. // case 19
  1245. {
  1246. args: []string{
  1247. "ldap-test",
  1248. "--id", "1",
  1249. },
  1250. existingAuthSource: &auth.Source{
  1251. Type: auth.PAM,
  1252. Cfg: &ldap.Source{},
  1253. },
  1254. errMsg: "invalid authentication type. expected: LDAP (simple auth), actual: PAM",
  1255. },
  1256. // case 20
  1257. {
  1258. args: []string{
  1259. "ldap-test",
  1260. "--id", "20",
  1261. "--name", "ldap (simple auth) flip 'active' attribute",
  1262. "--active",
  1263. },
  1264. id: 20,
  1265. existingAuthSource: &auth.Source{
  1266. Type: auth.DLDAP,
  1267. IsActive: false,
  1268. Cfg: &ldap.Source{
  1269. Name: "ldap (simple auth) flip 'active' attribute",
  1270. Enabled: true,
  1271. },
  1272. },
  1273. authSource: &auth.Source{
  1274. Type: auth.DLDAP,
  1275. Name: "ldap (simple auth) flip 'active' attribute",
  1276. IsActive: true,
  1277. Cfg: &ldap.Source{
  1278. Name: "ldap (simple auth) flip 'active' attribute",
  1279. Enabled: true,
  1280. },
  1281. },
  1282. },
  1283. }
  1284. for n, c := range cases {
  1285. // Mock functions.
  1286. var updatedAuthSource *auth.Source
  1287. service := &authService{
  1288. initDB: func(context.Context) error {
  1289. return nil
  1290. },
  1291. createAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  1292. assert.FailNow(t, "createAuthSource called", "case %d: should not call createAuthSource", n)
  1293. return nil
  1294. },
  1295. updateAuthSource: func(ctx context.Context, authSource *auth.Source) error {
  1296. updatedAuthSource = authSource
  1297. return nil
  1298. },
  1299. getAuthSourceByID: func(ctx context.Context, id int64) (*auth.Source, error) {
  1300. if c.id != 0 {
  1301. assert.Equal(t, c.id, id, "case %d: wrong id", n)
  1302. }
  1303. if c.existingAuthSource != nil {
  1304. return c.existingAuthSource, nil
  1305. }
  1306. return &auth.Source{
  1307. Type: auth.DLDAP,
  1308. Cfg: &ldap.Source{},
  1309. }, nil
  1310. },
  1311. }
  1312. // Create a copy of command to test
  1313. app := cli.Command{
  1314. Flags: microcmdAuthUpdateLdapSimpleAuth().Flags,
  1315. Action: service.updateLdapSimpleAuth,
  1316. }
  1317. // Run it
  1318. err := app.Run(t.Context(), c.args)
  1319. if c.errMsg != "" {
  1320. assert.EqualError(t, err, c.errMsg, "case %d: error should match", n)
  1321. } else {
  1322. assert.NoError(t, err, "case %d: should have no errors", n)
  1323. assert.Equal(t, c.authSource, updatedAuthSource, "case %d: wrong authSource", n)
  1324. }
  1325. }
  1326. }