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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. }
  143. falseTestCases := []string{
  144. "kb#2",
  145. "#2xy",
  146. }
  147. for _, testCase := range trueTestCases {
  148. assert.True(t, issueReferenceKeywordsPat.MatchString(testCase))
  149. }
  150. for _, testCase := range falseTestCases {
  151. assert.False(t, issueReferenceKeywordsPat.MatchString(testCase))
  152. }
  153. }
  154. func Test_getIssueFromRef(t *testing.T) {
  155. assert.NoError(t, PrepareTestDatabase())
  156. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  157. for _, test := range []struct {
  158. Ref string
  159. ExpectedIssueID int64
  160. }{
  161. {"#2", 2},
  162. {"reopen #2", 2},
  163. {"user2/repo2#1", 4},
  164. {"fixes user2/repo2#1", 4},
  165. {"fixes: user2/repo2#1", 4},
  166. } {
  167. issue, err := getIssueFromRef(repo, test.Ref)
  168. assert.NoError(t, err)
  169. if assert.NotNil(t, issue) {
  170. assert.EqualValues(t, test.ExpectedIssueID, issue.ID)
  171. }
  172. }
  173. for _, badRef := range []string{
  174. "doesnotexist/doesnotexist#1",
  175. fmt.Sprintf("#%d", NonexistentID),
  176. } {
  177. issue, err := getIssueFromRef(repo, badRef)
  178. assert.NoError(t, err)
  179. assert.Nil(t, issue)
  180. }
  181. }
  182. func TestUpdateIssuesCommit(t *testing.T) {
  183. assert.NoError(t, PrepareTestDatabase())
  184. pushCommits := []*PushCommit{
  185. {
  186. Sha1: "abcdef1",
  187. CommitterEmail: "user2@example.com",
  188. CommitterName: "User Two",
  189. AuthorEmail: "user4@example.com",
  190. AuthorName: "User Four",
  191. Message: "start working on #FST-1, #1",
  192. },
  193. {
  194. Sha1: "abcdef2",
  195. CommitterEmail: "user2@example.com",
  196. CommitterName: "User Two",
  197. AuthorEmail: "user2@example.com",
  198. AuthorName: "User Two",
  199. Message: "a plain message",
  200. },
  201. {
  202. Sha1: "abcdef2",
  203. CommitterEmail: "user2@example.com",
  204. CommitterName: "User Two",
  205. AuthorEmail: "user2@example.com",
  206. AuthorName: "User Two",
  207. Message: "close #2",
  208. },
  209. }
  210. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  211. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  212. repo.Owner = user
  213. commentBean := &Comment{
  214. Type: CommentTypeCommitRef,
  215. CommitSHA: "abcdef1",
  216. PosterID: user.ID,
  217. IssueID: 1,
  218. }
  219. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  220. AssertNotExistsBean(t, commentBean)
  221. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  222. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  223. AssertExistsAndLoadBean(t, commentBean)
  224. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  225. CheckConsistencyFor(t, &Action{})
  226. // Test that push to a non-default branch closes no issue.
  227. pushCommits = []*PushCommit{
  228. {
  229. Sha1: "abcdef1",
  230. CommitterEmail: "user2@example.com",
  231. CommitterName: "User Two",
  232. AuthorEmail: "user4@example.com",
  233. AuthorName: "User Four",
  234. Message: "close #1",
  235. },
  236. }
  237. repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  238. commentBean = &Comment{
  239. Type: CommentTypeCommitRef,
  240. CommitSHA: "abcdef1",
  241. PosterID: user.ID,
  242. IssueID: 6,
  243. }
  244. issueBean = &Issue{RepoID: repo.ID, Index: 1}
  245. AssertNotExistsBean(t, commentBean)
  246. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  247. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  248. AssertExistsAndLoadBean(t, commentBean)
  249. AssertNotExistsBean(t, issueBean, "is_closed=1")
  250. CheckConsistencyFor(t, &Action{})
  251. }
  252. func TestUpdateIssuesCommit_Colon(t *testing.T) {
  253. assert.NoError(t, PrepareTestDatabase())
  254. pushCommits := []*PushCommit{
  255. {
  256. Sha1: "abcdef2",
  257. CommitterEmail: "user2@example.com",
  258. CommitterName: "User Two",
  259. AuthorEmail: "user2@example.com",
  260. AuthorName: "User Two",
  261. Message: "close: #2",
  262. },
  263. }
  264. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  265. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  266. repo.Owner = user
  267. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  268. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  269. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  270. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  271. CheckConsistencyFor(t, &Action{})
  272. }
  273. func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  274. assert.NoError(t, PrepareTestDatabase())
  275. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  276. // Test that push to a non-default branch closes an issue.
  277. pushCommits := []*PushCommit{
  278. {
  279. Sha1: "abcdef1",
  280. CommitterEmail: "user2@example.com",
  281. CommitterName: "User Two",
  282. AuthorEmail: "user4@example.com",
  283. AuthorName: "User Four",
  284. Message: "close #2",
  285. },
  286. }
  287. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  288. commentBean := &Comment{
  289. Type: CommentTypeCommitRef,
  290. CommitSHA: "abcdef1",
  291. PosterID: user.ID,
  292. IssueID: 7,
  293. }
  294. issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
  295. AssertNotExistsBean(t, commentBean)
  296. AssertNotExistsBean(t, issueBean, "is_closed=1")
  297. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  298. AssertExistsAndLoadBean(t, commentBean)
  299. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  300. CheckConsistencyFor(t, &Action{})
  301. }
  302. func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
  303. assert.NoError(t, PrepareTestDatabase())
  304. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  305. // Test that a push to default branch closes issue in another repo
  306. // If the user also has push permissions to that repo
  307. pushCommits := []*PushCommit{
  308. {
  309. Sha1: "abcdef1",
  310. CommitterEmail: "user2@example.com",
  311. CommitterName: "User Two",
  312. AuthorEmail: "user2@example.com",
  313. AuthorName: "User Two",
  314. Message: "close user2/repo1#1",
  315. },
  316. }
  317. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  318. commentBean := &Comment{
  319. Type: CommentTypeCommitRef,
  320. CommitSHA: "abcdef1",
  321. PosterID: user.ID,
  322. IssueID: 1,
  323. }
  324. issueBean := &Issue{RepoID: 1, Index: 1, ID: 1}
  325. AssertNotExistsBean(t, commentBean)
  326. AssertNotExistsBean(t, issueBean, "is_closed=1")
  327. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  328. AssertExistsAndLoadBean(t, commentBean)
  329. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  330. CheckConsistencyFor(t, &Action{})
  331. }
  332. func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
  333. assert.NoError(t, PrepareTestDatabase())
  334. user := AssertExistsAndLoadBean(t, &User{ID: 10}).(*User)
  335. // Test that a push with close reference *can not* close issue
  336. // If the commiter doesn't have push rights in that repo
  337. pushCommits := []*PushCommit{
  338. {
  339. Sha1: "abcdef3",
  340. CommitterEmail: "user10@example.com",
  341. CommitterName: "User Ten",
  342. AuthorEmail: "user10@example.com",
  343. AuthorName: "User Ten",
  344. Message: "close user3/repo3#1",
  345. },
  346. }
  347. repo := AssertExistsAndLoadBean(t, &Repository{ID: 6}).(*Repository)
  348. commentBean := &Comment{
  349. Type: CommentTypeCommitRef,
  350. CommitSHA: "abcdef3",
  351. PosterID: user.ID,
  352. IssueID: 6,
  353. }
  354. issueBean := &Issue{RepoID: 3, Index: 1, ID: 6}
  355. AssertNotExistsBean(t, commentBean)
  356. AssertNotExistsBean(t, issueBean, "is_closed=1")
  357. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  358. AssertExistsAndLoadBean(t, commentBean)
  359. AssertNotExistsBean(t, issueBean, "is_closed=1")
  360. CheckConsistencyFor(t, &Action{})
  361. }
  362. func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
  363. AssertNotExistsBean(t, actionBean)
  364. assert.NoError(t, CommitRepoAction(opts))
  365. AssertExistsAndLoadBean(t, actionBean)
  366. CheckConsistencyFor(t, &Action{})
  367. }
  368. func TestCommitRepoAction(t *testing.T) {
  369. samples := []struct {
  370. userID int64
  371. repositoryID int64
  372. commitRepoActionOptions CommitRepoActionOptions
  373. action Action
  374. }{
  375. {
  376. userID: 2,
  377. repositoryID: 2,
  378. commitRepoActionOptions: CommitRepoActionOptions{
  379. RefFullName: "refName",
  380. OldCommitID: "oldCommitID",
  381. NewCommitID: "newCommitID",
  382. Commits: &PushCommits{
  383. avatars: make(map[string]string),
  384. Commits: []*PushCommit{
  385. {
  386. Sha1: "abcdef1",
  387. CommitterEmail: "user2@example.com",
  388. CommitterName: "User Two",
  389. AuthorEmail: "user4@example.com",
  390. AuthorName: "User Four",
  391. Message: "message1",
  392. },
  393. {
  394. Sha1: "abcdef2",
  395. CommitterEmail: "user2@example.com",
  396. CommitterName: "User Two",
  397. AuthorEmail: "user2@example.com",
  398. AuthorName: "User Two",
  399. Message: "message2",
  400. },
  401. },
  402. Len: 2,
  403. },
  404. },
  405. action: Action{
  406. OpType: ActionCommitRepo,
  407. RefName: "refName",
  408. },
  409. },
  410. {
  411. userID: 2,
  412. repositoryID: 1,
  413. commitRepoActionOptions: CommitRepoActionOptions{
  414. RefFullName: git.TagPrefix + "v1.1",
  415. OldCommitID: git.EmptySHA,
  416. NewCommitID: "newCommitID",
  417. Commits: &PushCommits{},
  418. },
  419. action: Action{
  420. OpType: ActionPushTag,
  421. RefName: "v1.1",
  422. },
  423. },
  424. {
  425. userID: 2,
  426. repositoryID: 1,
  427. commitRepoActionOptions: CommitRepoActionOptions{
  428. RefFullName: git.TagPrefix + "v1.1",
  429. OldCommitID: "oldCommitID",
  430. NewCommitID: git.EmptySHA,
  431. Commits: &PushCommits{},
  432. },
  433. action: Action{
  434. OpType: ActionDeleteTag,
  435. RefName: "v1.1",
  436. },
  437. },
  438. {
  439. userID: 2,
  440. repositoryID: 1,
  441. commitRepoActionOptions: CommitRepoActionOptions{
  442. RefFullName: git.BranchPrefix + "feature/1",
  443. OldCommitID: "oldCommitID",
  444. NewCommitID: git.EmptySHA,
  445. Commits: &PushCommits{},
  446. },
  447. action: Action{
  448. OpType: ActionDeleteBranch,
  449. RefName: "feature/1",
  450. },
  451. },
  452. }
  453. for _, s := range samples {
  454. PrepareTestEnv(t)
  455. user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
  456. repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
  457. repo.Owner = user
  458. s.commitRepoActionOptions.PusherName = user.Name
  459. s.commitRepoActionOptions.RepoOwnerID = user.ID
  460. s.commitRepoActionOptions.RepoName = repo.Name
  461. s.action.ActUserID = user.ID
  462. s.action.RepoID = repo.ID
  463. s.action.Repo = repo
  464. s.action.IsPrivate = repo.IsPrivate
  465. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  466. }
  467. }
  468. func TestTransferRepoAction(t *testing.T) {
  469. assert.NoError(t, PrepareTestDatabase())
  470. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  471. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  472. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  473. repo.OwnerID = user4.ID
  474. repo.Owner = user4
  475. actionBean := &Action{
  476. OpType: ActionTransferRepo,
  477. ActUserID: user2.ID,
  478. ActUser: user2,
  479. RepoID: repo.ID,
  480. Repo: repo,
  481. IsPrivate: repo.IsPrivate,
  482. }
  483. AssertNotExistsBean(t, actionBean)
  484. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  485. AssertExistsAndLoadBean(t, actionBean)
  486. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  487. assert.NoError(t, err)
  488. CheckConsistencyFor(t, &Action{})
  489. }
  490. func TestMergePullRequestAction(t *testing.T) {
  491. assert.NoError(t, PrepareTestDatabase())
  492. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  493. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  494. repo.Owner = user
  495. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  496. actionBean := &Action{
  497. OpType: ActionMergePullRequest,
  498. ActUserID: user.ID,
  499. ActUser: user,
  500. RepoID: repo.ID,
  501. Repo: repo,
  502. IsPrivate: repo.IsPrivate,
  503. }
  504. AssertNotExistsBean(t, actionBean)
  505. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  506. AssertExistsAndLoadBean(t, actionBean)
  507. CheckConsistencyFor(t, &Action{})
  508. }
  509. func TestGetFeeds(t *testing.T) {
  510. // test with an individual user
  511. assert.NoError(t, PrepareTestDatabase())
  512. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  513. actions, err := GetFeeds(GetFeedsOptions{
  514. RequestedUser: user,
  515. RequestingUserID: user.ID,
  516. IncludePrivate: true,
  517. OnlyPerformedBy: false,
  518. IncludeDeleted: true,
  519. })
  520. assert.NoError(t, err)
  521. if assert.Len(t, actions, 1) {
  522. assert.EqualValues(t, 1, actions[0].ID)
  523. assert.EqualValues(t, user.ID, actions[0].UserID)
  524. }
  525. actions, err = GetFeeds(GetFeedsOptions{
  526. RequestedUser: user,
  527. RequestingUserID: user.ID,
  528. IncludePrivate: false,
  529. OnlyPerformedBy: false,
  530. })
  531. assert.NoError(t, err)
  532. assert.Len(t, actions, 0)
  533. }
  534. func TestGetFeeds2(t *testing.T) {
  535. // test with an organization user
  536. assert.NoError(t, PrepareTestDatabase())
  537. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  538. const userID = 2 // user2 is an owner of the organization
  539. actions, err := GetFeeds(GetFeedsOptions{
  540. RequestedUser: org,
  541. RequestingUserID: userID,
  542. IncludePrivate: true,
  543. OnlyPerformedBy: false,
  544. IncludeDeleted: true,
  545. })
  546. assert.NoError(t, err)
  547. assert.Len(t, actions, 1)
  548. if assert.Len(t, actions, 1) {
  549. assert.EqualValues(t, 2, actions[0].ID)
  550. assert.EqualValues(t, org.ID, actions[0].UserID)
  551. }
  552. actions, err = GetFeeds(GetFeedsOptions{
  553. RequestedUser: org,
  554. RequestingUserID: userID,
  555. IncludePrivate: false,
  556. OnlyPerformedBy: false,
  557. IncludeDeleted: true,
  558. })
  559. assert.NoError(t, err)
  560. assert.Len(t, actions, 0)
  561. }