You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action_test.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. package models
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "testing"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestAction_GetRepoPath(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  14. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  15. action := &Action{RepoID: repo.ID}
  16. assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
  17. }
  18. func TestAction_GetRepoLink(t *testing.T) {
  19. assert.NoError(t, PrepareTestDatabase())
  20. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  21. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  22. action := &Action{RepoID: repo.ID}
  23. setting.AppSubURL = "/suburl/"
  24. expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
  25. assert.Equal(t, expected, action.GetRepoLink())
  26. }
  27. func TestNewRepoAction(t *testing.T) {
  28. assert.NoError(t, PrepareTestDatabase())
  29. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  30. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  31. repo.Owner = user
  32. actionBean := &Action{
  33. OpType: ActionCreateRepo,
  34. ActUserID: user.ID,
  35. RepoID: repo.ID,
  36. ActUser: user,
  37. Repo: repo,
  38. IsPrivate: repo.IsPrivate,
  39. }
  40. AssertNotExistsBean(t, actionBean)
  41. assert.NoError(t, NewRepoAction(user, repo))
  42. AssertExistsAndLoadBean(t, actionBean)
  43. CheckConsistencyFor(t, &Action{})
  44. }
  45. func TestRenameRepoAction(t *testing.T) {
  46. assert.NoError(t, PrepareTestDatabase())
  47. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  48. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  49. repo.Owner = user
  50. oldRepoName := repo.Name
  51. const newRepoName = "newRepoName"
  52. repo.Name = newRepoName
  53. repo.LowerName = strings.ToLower(newRepoName)
  54. actionBean := &Action{
  55. OpType: ActionRenameRepo,
  56. ActUserID: user.ID,
  57. ActUser: user,
  58. RepoID: repo.ID,
  59. Repo: repo,
  60. IsPrivate: repo.IsPrivate,
  61. Content: oldRepoName,
  62. }
  63. AssertNotExistsBean(t, actionBean)
  64. assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
  65. AssertExistsAndLoadBean(t, actionBean)
  66. _, err := x.ID(repo.ID).Cols("name", "lower_name").Update(repo)
  67. assert.NoError(t, err)
  68. CheckConsistencyFor(t, &Action{})
  69. }
  70. func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
  71. pushCommits := NewPushCommits()
  72. pushCommits.Commits = []*PushCommit{
  73. {
  74. Sha1: "abcdef1",
  75. CommitterEmail: "user2@example.com",
  76. CommitterName: "User Two",
  77. AuthorEmail: "user4@example.com",
  78. AuthorName: "User Four",
  79. Message: "message1",
  80. },
  81. {
  82. Sha1: "abcdef2",
  83. CommitterEmail: "user2@example.com",
  84. CommitterName: "User Two",
  85. AuthorEmail: "user2@example.com",
  86. AuthorName: "User Two",
  87. Message: "message2",
  88. },
  89. }
  90. pushCommits.Len = len(pushCommits.Commits)
  91. payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
  92. if assert.Len(t, payloadCommits, 2) {
  93. assert.Equal(t, "abcdef1", payloadCommits[0].ID)
  94. assert.Equal(t, "message1", payloadCommits[0].Message)
  95. assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
  96. assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
  97. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  98. assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
  99. assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
  100. assert.Equal(t, "abcdef2", payloadCommits[1].ID)
  101. assert.Equal(t, "message2", payloadCommits[1].Message)
  102. assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
  103. assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
  104. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  105. assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
  106. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  107. }
  108. }
  109. func TestPushCommits_AvatarLink(t *testing.T) {
  110. pushCommits := NewPushCommits()
  111. pushCommits.Commits = []*PushCommit{
  112. {
  113. Sha1: "abcdef1",
  114. CommitterEmail: "user2@example.com",
  115. CommitterName: "User Two",
  116. AuthorEmail: "user4@example.com",
  117. AuthorName: "User Four",
  118. Message: "message1",
  119. },
  120. {
  121. Sha1: "abcdef2",
  122. CommitterEmail: "user2@example.com",
  123. CommitterName: "User Two",
  124. AuthorEmail: "user2@example.com",
  125. AuthorName: "User Two",
  126. Message: "message2",
  127. },
  128. }
  129. pushCommits.Len = len(pushCommits.Commits)
  130. assert.Equal(t,
  131. "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
  132. pushCommits.AvatarLink("user2@example.com"))
  133. assert.Equal(t,
  134. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
  135. pushCommits.AvatarLink("nonexistent@example.com"))
  136. }
  137. func TestRegExp_issueReferenceKeywordsPat(t *testing.T) {
  138. trueTestCases := []string{
  139. "#2",
  140. "[#2]",
  141. "please see go-gitea/gitea#5",
  142. "#2:",
  143. }
  144. falseTestCases := []string{
  145. "kb#2",
  146. "#2xy",
  147. }
  148. for _, testCase := range trueTestCases {
  149. assert.True(t, issueReferenceKeywordsPat.MatchString(testCase))
  150. }
  151. for _, testCase := range falseTestCases {
  152. assert.False(t, issueReferenceKeywordsPat.MatchString(testCase))
  153. }
  154. }
  155. func Test_getIssueFromRef(t *testing.T) {
  156. assert.NoError(t, PrepareTestDatabase())
  157. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  158. for _, test := range []struct {
  159. Ref string
  160. ExpectedIssueID int64
  161. }{
  162. {"#2", 2},
  163. {"reopen #2", 2},
  164. {"user2/repo2#1", 4},
  165. {"fixes user2/repo2#1", 4},
  166. {"fixes: user2/repo2#1", 4},
  167. } {
  168. issue, err := getIssueFromRef(repo, test.Ref)
  169. assert.NoError(t, err)
  170. if assert.NotNil(t, issue) {
  171. assert.EqualValues(t, test.ExpectedIssueID, issue.ID)
  172. }
  173. }
  174. for _, badRef := range []string{
  175. "doesnotexist/doesnotexist#1",
  176. fmt.Sprintf("#%d", NonexistentID),
  177. } {
  178. issue, err := getIssueFromRef(repo, badRef)
  179. assert.NoError(t, err)
  180. assert.Nil(t, issue)
  181. }
  182. }
  183. func TestUpdateIssuesCommit(t *testing.T) {
  184. assert.NoError(t, PrepareTestDatabase())
  185. pushCommits := []*PushCommit{
  186. {
  187. Sha1: "abcdef1",
  188. CommitterEmail: "user2@example.com",
  189. CommitterName: "User Two",
  190. AuthorEmail: "user4@example.com",
  191. AuthorName: "User Four",
  192. Message: "start working on #FST-1, #1",
  193. },
  194. {
  195. Sha1: "abcdef2",
  196. CommitterEmail: "user2@example.com",
  197. CommitterName: "User Two",
  198. AuthorEmail: "user2@example.com",
  199. AuthorName: "User Two",
  200. Message: "a plain message",
  201. },
  202. {
  203. Sha1: "abcdef2",
  204. CommitterEmail: "user2@example.com",
  205. CommitterName: "User Two",
  206. AuthorEmail: "user2@example.com",
  207. AuthorName: "User Two",
  208. Message: "close #2",
  209. },
  210. }
  211. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  212. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  213. repo.Owner = user
  214. commentBean := &Comment{
  215. Type: CommentTypeCommitRef,
  216. CommitSHA: "abcdef1",
  217. PosterID: user.ID,
  218. IssueID: 1,
  219. }
  220. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  221. AssertNotExistsBean(t, commentBean)
  222. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  223. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  224. AssertExistsAndLoadBean(t, commentBean)
  225. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  226. CheckConsistencyFor(t, &Action{})
  227. // Test that push to a non-default branch closes no issue.
  228. pushCommits = []*PushCommit{
  229. {
  230. Sha1: "abcdef1",
  231. CommitterEmail: "user2@example.com",
  232. CommitterName: "User Two",
  233. AuthorEmail: "user4@example.com",
  234. AuthorName: "User Four",
  235. Message: "close #1",
  236. },
  237. }
  238. repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  239. commentBean = &Comment{
  240. Type: CommentTypeCommitRef,
  241. CommitSHA: "abcdef1",
  242. PosterID: user.ID,
  243. IssueID: 6,
  244. }
  245. issueBean = &Issue{RepoID: repo.ID, Index: 1}
  246. AssertNotExistsBean(t, commentBean)
  247. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  248. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  249. AssertExistsAndLoadBean(t, commentBean)
  250. AssertNotExistsBean(t, issueBean, "is_closed=1")
  251. CheckConsistencyFor(t, &Action{})
  252. }
  253. func TestUpdateIssuesCommit_Colon(t *testing.T) {
  254. assert.NoError(t, PrepareTestDatabase())
  255. pushCommits := []*PushCommit{
  256. {
  257. Sha1: "abcdef2",
  258. CommitterEmail: "user2@example.com",
  259. CommitterName: "User Two",
  260. AuthorEmail: "user2@example.com",
  261. AuthorName: "User Two",
  262. Message: "close: #2",
  263. },
  264. }
  265. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  266. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  267. repo.Owner = user
  268. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  269. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  270. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  271. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  272. CheckConsistencyFor(t, &Action{})
  273. }
  274. func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  275. assert.NoError(t, PrepareTestDatabase())
  276. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  277. // Test that push to a non-default branch closes an issue.
  278. pushCommits := []*PushCommit{
  279. {
  280. Sha1: "abcdef1",
  281. CommitterEmail: "user2@example.com",
  282. CommitterName: "User Two",
  283. AuthorEmail: "user4@example.com",
  284. AuthorName: "User Four",
  285. Message: "close #2",
  286. },
  287. }
  288. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  289. commentBean := &Comment{
  290. Type: CommentTypeCommitRef,
  291. CommitSHA: "abcdef1",
  292. PosterID: user.ID,
  293. IssueID: 7,
  294. }
  295. issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
  296. AssertNotExistsBean(t, commentBean)
  297. AssertNotExistsBean(t, issueBean, "is_closed=1")
  298. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  299. AssertExistsAndLoadBean(t, commentBean)
  300. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  301. CheckConsistencyFor(t, &Action{})
  302. }
  303. func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
  304. assert.NoError(t, PrepareTestDatabase())
  305. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  306. // Test that a push to default branch closes issue in another repo
  307. // If the user also has push permissions to that repo
  308. pushCommits := []*PushCommit{
  309. {
  310. Sha1: "abcdef1",
  311. CommitterEmail: "user2@example.com",
  312. CommitterName: "User Two",
  313. AuthorEmail: "user2@example.com",
  314. AuthorName: "User Two",
  315. Message: "close user2/repo1#1",
  316. },
  317. }
  318. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  319. commentBean := &Comment{
  320. Type: CommentTypeCommitRef,
  321. CommitSHA: "abcdef1",
  322. PosterID: user.ID,
  323. IssueID: 1,
  324. }
  325. issueBean := &Issue{RepoID: 1, Index: 1, ID: 1}
  326. AssertNotExistsBean(t, commentBean)
  327. AssertNotExistsBean(t, issueBean, "is_closed=1")
  328. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  329. AssertExistsAndLoadBean(t, commentBean)
  330. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  331. CheckConsistencyFor(t, &Action{})
  332. }
  333. func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
  334. assert.NoError(t, PrepareTestDatabase())
  335. user := AssertExistsAndLoadBean(t, &User{ID: 10}).(*User)
  336. // Test that a push with close reference *can not* close issue
  337. // If the commiter doesn't have push rights in that repo
  338. pushCommits := []*PushCommit{
  339. {
  340. Sha1: "abcdef3",
  341. CommitterEmail: "user10@example.com",
  342. CommitterName: "User Ten",
  343. AuthorEmail: "user10@example.com",
  344. AuthorName: "User Ten",
  345. Message: "close user3/repo3#1",
  346. },
  347. }
  348. repo := AssertExistsAndLoadBean(t, &Repository{ID: 6}).(*Repository)
  349. commentBean := &Comment{
  350. Type: CommentTypeCommitRef,
  351. CommitSHA: "abcdef3",
  352. PosterID: user.ID,
  353. IssueID: 6,
  354. }
  355. issueBean := &Issue{RepoID: 3, Index: 1, ID: 6}
  356. AssertNotExistsBean(t, commentBean)
  357. AssertNotExistsBean(t, issueBean, "is_closed=1")
  358. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  359. AssertExistsAndLoadBean(t, commentBean)
  360. AssertNotExistsBean(t, issueBean, "is_closed=1")
  361. CheckConsistencyFor(t, &Action{})
  362. }
  363. func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
  364. AssertNotExistsBean(t, actionBean)
  365. assert.NoError(t, CommitRepoAction(opts))
  366. AssertExistsAndLoadBean(t, actionBean)
  367. CheckConsistencyFor(t, &Action{})
  368. }
  369. func TestCommitRepoAction(t *testing.T) {
  370. samples := []struct {
  371. userID int64
  372. repositoryID int64
  373. commitRepoActionOptions CommitRepoActionOptions
  374. action Action
  375. }{
  376. {
  377. userID: 2,
  378. repositoryID: 2,
  379. commitRepoActionOptions: CommitRepoActionOptions{
  380. RefFullName: "refName",
  381. OldCommitID: "oldCommitID",
  382. NewCommitID: "newCommitID",
  383. Commits: &PushCommits{
  384. avatars: make(map[string]string),
  385. Commits: []*PushCommit{
  386. {
  387. Sha1: "abcdef1",
  388. CommitterEmail: "user2@example.com",
  389. CommitterName: "User Two",
  390. AuthorEmail: "user4@example.com",
  391. AuthorName: "User Four",
  392. Message: "message1",
  393. },
  394. {
  395. Sha1: "abcdef2",
  396. CommitterEmail: "user2@example.com",
  397. CommitterName: "User Two",
  398. AuthorEmail: "user2@example.com",
  399. AuthorName: "User Two",
  400. Message: "message2",
  401. },
  402. },
  403. Len: 2,
  404. },
  405. },
  406. action: Action{
  407. OpType: ActionCommitRepo,
  408. RefName: "refName",
  409. },
  410. },
  411. {
  412. userID: 2,
  413. repositoryID: 1,
  414. commitRepoActionOptions: CommitRepoActionOptions{
  415. RefFullName: git.TagPrefix + "v1.1",
  416. OldCommitID: git.EmptySHA,
  417. NewCommitID: "newCommitID",
  418. Commits: &PushCommits{},
  419. },
  420. action: Action{
  421. OpType: ActionPushTag,
  422. RefName: "v1.1",
  423. },
  424. },
  425. {
  426. userID: 2,
  427. repositoryID: 1,
  428. commitRepoActionOptions: CommitRepoActionOptions{
  429. RefFullName: git.TagPrefix + "v1.1",
  430. OldCommitID: "oldCommitID",
  431. NewCommitID: git.EmptySHA,
  432. Commits: &PushCommits{},
  433. },
  434. action: Action{
  435. OpType: ActionDeleteTag,
  436. RefName: "v1.1",
  437. },
  438. },
  439. {
  440. userID: 2,
  441. repositoryID: 1,
  442. commitRepoActionOptions: CommitRepoActionOptions{
  443. RefFullName: git.BranchPrefix + "feature/1",
  444. OldCommitID: "oldCommitID",
  445. NewCommitID: git.EmptySHA,
  446. Commits: &PushCommits{},
  447. },
  448. action: Action{
  449. OpType: ActionDeleteBranch,
  450. RefName: "feature/1",
  451. },
  452. },
  453. }
  454. for _, s := range samples {
  455. PrepareTestEnv(t)
  456. user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
  457. repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
  458. repo.Owner = user
  459. s.commitRepoActionOptions.PusherName = user.Name
  460. s.commitRepoActionOptions.RepoOwnerID = user.ID
  461. s.commitRepoActionOptions.RepoName = repo.Name
  462. s.action.ActUserID = user.ID
  463. s.action.RepoID = repo.ID
  464. s.action.Repo = repo
  465. s.action.IsPrivate = repo.IsPrivate
  466. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  467. }
  468. }
  469. func TestTransferRepoAction(t *testing.T) {
  470. assert.NoError(t, PrepareTestDatabase())
  471. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  472. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  473. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  474. repo.OwnerID = user4.ID
  475. repo.Owner = user4
  476. actionBean := &Action{
  477. OpType: ActionTransferRepo,
  478. ActUserID: user2.ID,
  479. ActUser: user2,
  480. RepoID: repo.ID,
  481. Repo: repo,
  482. IsPrivate: repo.IsPrivate,
  483. }
  484. AssertNotExistsBean(t, actionBean)
  485. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  486. AssertExistsAndLoadBean(t, actionBean)
  487. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  488. assert.NoError(t, err)
  489. CheckConsistencyFor(t, &Action{})
  490. }
  491. func TestMergePullRequestAction(t *testing.T) {
  492. assert.NoError(t, PrepareTestDatabase())
  493. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  494. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  495. repo.Owner = user
  496. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  497. actionBean := &Action{
  498. OpType: ActionMergePullRequest,
  499. ActUserID: user.ID,
  500. ActUser: user,
  501. RepoID: repo.ID,
  502. Repo: repo,
  503. IsPrivate: repo.IsPrivate,
  504. }
  505. AssertNotExistsBean(t, actionBean)
  506. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  507. AssertExistsAndLoadBean(t, actionBean)
  508. CheckConsistencyFor(t, &Action{})
  509. }
  510. func TestGetFeeds(t *testing.T) {
  511. // test with an individual user
  512. assert.NoError(t, PrepareTestDatabase())
  513. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  514. actions, err := GetFeeds(GetFeedsOptions{
  515. RequestedUser: user,
  516. RequestingUserID: user.ID,
  517. IncludePrivate: true,
  518. OnlyPerformedBy: false,
  519. IncludeDeleted: true,
  520. })
  521. assert.NoError(t, err)
  522. if assert.Len(t, actions, 1) {
  523. assert.EqualValues(t, 1, actions[0].ID)
  524. assert.EqualValues(t, user.ID, actions[0].UserID)
  525. }
  526. actions, err = GetFeeds(GetFeedsOptions{
  527. RequestedUser: user,
  528. RequestingUserID: user.ID,
  529. IncludePrivate: false,
  530. OnlyPerformedBy: false,
  531. })
  532. assert.NoError(t, err)
  533. assert.Len(t, actions, 0)
  534. }
  535. func TestGetFeeds2(t *testing.T) {
  536. // test with an organization user
  537. assert.NoError(t, PrepareTestDatabase())
  538. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  539. const userID = 2 // user2 is an owner of the organization
  540. actions, err := GetFeeds(GetFeedsOptions{
  541. RequestedUser: org,
  542. RequestingUserID: userID,
  543. IncludePrivate: true,
  544. OnlyPerformedBy: false,
  545. IncludeDeleted: true,
  546. })
  547. assert.NoError(t, err)
  548. assert.Len(t, actions, 1)
  549. if assert.Len(t, actions, 1) {
  550. assert.EqualValues(t, 2, actions[0].ID)
  551. assert.EqualValues(t, org.ID, actions[0].UserID)
  552. }
  553. actions, err = GetFeeds(GetFeedsOptions{
  554. RequestedUser: org,
  555. RequestingUserID: userID,
  556. IncludePrivate: false,
  557. OnlyPerformedBy: false,
  558. IncludeDeleted: true,
  559. })
  560. assert.NoError(t, err)
  561. assert.Len(t, actions, 0)
  562. }