|
| 1 | +// Copyright 2020 The Gitea 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 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models" |
| 13 | + api "code.gitea.io/gitea/modules/structs" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAPINotification(t *testing.T) { |
| 19 | + defer prepareTestEnv(t)() |
| 20 | + |
| 21 | + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) |
| 22 | + repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 23 | + thread5 := models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) |
| 24 | + assert.NoError(t, thread5.LoadAttributes()) |
| 25 | + session := loginUser(t, user2.Name) |
| 26 | + token := getTokenForLoggedInUser(t, session) |
| 27 | + |
| 28 | + // -- GET /notifications -- |
| 29 | + // test filter |
| 30 | + since := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 |
| 31 | + req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?since=%s&token=%s", since, token)) |
| 32 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 33 | + var apiNL []api.NotificationThread |
| 34 | + DecodeJSON(t, resp, &apiNL) |
| 35 | + |
| 36 | + assert.Len(t, apiNL, 1) |
| 37 | + assert.EqualValues(t, 5, apiNL[0].ID) |
| 38 | + |
| 39 | + // test filter |
| 40 | + before := "2000-01-01T01%3A06%3A59%2B00%3A00" //946688819 |
| 41 | + |
| 42 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=%s&before=%s&token=%s", "true", before, token)) |
| 43 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 44 | + DecodeJSON(t, resp, &apiNL) |
| 45 | + |
| 46 | + assert.Len(t, apiNL, 3) |
| 47 | + assert.EqualValues(t, 4, apiNL[0].ID) |
| 48 | + assert.EqualValues(t, true, apiNL[0].Unread) |
| 49 | + assert.EqualValues(t, false, apiNL[0].Pinned) |
| 50 | + assert.EqualValues(t, 3, apiNL[1].ID) |
| 51 | + assert.EqualValues(t, false, apiNL[1].Unread) |
| 52 | + assert.EqualValues(t, true, apiNL[1].Pinned) |
| 53 | + assert.EqualValues(t, 2, apiNL[2].ID) |
| 54 | + assert.EqualValues(t, false, apiNL[2].Unread) |
| 55 | + assert.EqualValues(t, false, apiNL[2].Pinned) |
| 56 | + |
| 57 | + // -- GET /repos/{owner}/{repo}/notifications -- |
| 58 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?token=%s", user2.Name, repo1.Name, token)) |
| 59 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 60 | + DecodeJSON(t, resp, &apiNL) |
| 61 | + |
| 62 | + assert.Len(t, apiNL, 1) |
| 63 | + assert.EqualValues(t, 4, apiNL[0].ID) |
| 64 | + |
| 65 | + // -- GET /notifications/threads/{id} -- |
| 66 | + // get forbidden |
| 67 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", 1, token)) |
| 68 | + resp = session.MakeRequest(t, req, http.StatusForbidden) |
| 69 | + |
| 70 | + // get own |
| 71 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token)) |
| 72 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 73 | + var apiN api.NotificationThread |
| 74 | + DecodeJSON(t, resp, &apiN) |
| 75 | + |
| 76 | + assert.EqualValues(t, 5, apiN.ID) |
| 77 | + assert.EqualValues(t, false, apiN.Pinned) |
| 78 | + assert.EqualValues(t, true, apiN.Unread) |
| 79 | + assert.EqualValues(t, "issue4", apiN.Subject.Title) |
| 80 | + assert.EqualValues(t, "Issue", apiN.Subject.Type) |
| 81 | + assert.EqualValues(t, thread5.Issue.APIURL(), apiN.Subject.URL) |
| 82 | + assert.EqualValues(t, thread5.Repository.HTMLURL(), apiN.Repository.HTMLURL) |
| 83 | + |
| 84 | + // -- mark notifications as read -- |
| 85 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token)) |
| 86 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 87 | + DecodeJSON(t, resp, &apiNL) |
| 88 | + assert.Len(t, apiNL, 2) |
| 89 | + |
| 90 | + lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ... |
| 91 | + req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token)) |
| 92 | + resp = session.MakeRequest(t, req, http.StatusResetContent) |
| 93 | + |
| 94 | + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token)) |
| 95 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 96 | + DecodeJSON(t, resp, &apiNL) |
| 97 | + assert.Len(t, apiNL, 1) |
| 98 | + |
| 99 | + // -- PATCH /notifications/threads/{id} -- |
| 100 | + req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/notifications/threads/%d?token=%s", thread5.ID, token)) |
| 101 | + resp = session.MakeRequest(t, req, http.StatusResetContent) |
| 102 | + |
| 103 | + assert.Equal(t, models.NotificationStatusUnread, thread5.Status) |
| 104 | + thread5 = models.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) |
| 105 | + assert.Equal(t, models.NotificationStatusRead, thread5.Status) |
| 106 | +} |
0 commit comments