code.gitea.io/gitea@v1.22.3/tests/integration/api_issue_reaction_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  	"time"
    11  
    12  	auth_model "code.gitea.io/gitea/models/auth"
    13  	"code.gitea.io/gitea/models/db"
    14  	issues_model "code.gitea.io/gitea/models/issues"
    15  	repo_model "code.gitea.io/gitea/models/repo"
    16  	"code.gitea.io/gitea/models/unittest"
    17  	user_model "code.gitea.io/gitea/models/user"
    18  	api "code.gitea.io/gitea/modules/structs"
    19  	"code.gitea.io/gitea/services/convert"
    20  	"code.gitea.io/gitea/tests"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestAPIIssuesReactions(t *testing.T) {
    26  	defer tests.PrepareTestEnv(t)()
    27  
    28  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
    29  	_ = issue.LoadRepo(db.DefaultContext)
    30  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
    31  
    32  	session := loginUser(t, owner.Name)
    33  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    34  
    35  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    36  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions", owner.Name, issue.Repo.Name, issue.Index)
    37  
    38  	// Try to add not allowed reaction
    39  	req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
    40  		Reaction: "wrong",
    41  	}).AddTokenAuth(token)
    42  	MakeRequest(t, req, http.StatusForbidden)
    43  
    44  	// Delete not allowed reaction
    45  	req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
    46  		Reaction: "zzz",
    47  	}).AddTokenAuth(token)
    48  	MakeRequest(t, req, http.StatusOK)
    49  
    50  	// Add allowed reaction
    51  	req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
    52  		Reaction: "rocket",
    53  	}).AddTokenAuth(token)
    54  	resp := MakeRequest(t, req, http.StatusCreated)
    55  	var apiNewReaction api.Reaction
    56  	DecodeJSON(t, resp, &apiNewReaction)
    57  
    58  	// Add existing reaction
    59  	MakeRequest(t, req, http.StatusForbidden)
    60  
    61  	// Blocked user can't react to comment
    62  	user34 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 34})
    63  	req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
    64  		Reaction: "rocket",
    65  	}).AddTokenAuth(getUserToken(t, user34.Name, auth_model.AccessTokenScopeWriteIssue))
    66  	MakeRequest(t, req, http.StatusForbidden)
    67  
    68  	// Get end result of reaction list of issue #1
    69  	req = NewRequest(t, "GET", urlStr).
    70  		AddTokenAuth(token)
    71  	resp = MakeRequest(t, req, http.StatusOK)
    72  	var apiReactions []*api.Reaction
    73  	DecodeJSON(t, resp, &apiReactions)
    74  	expectResponse := make(map[int]api.Reaction)
    75  	expectResponse[0] = api.Reaction{
    76  		User:     convert.ToUser(db.DefaultContext, user2, user2),
    77  		Reaction: "eyes",
    78  		Created:  time.Unix(1573248003, 0),
    79  	}
    80  	expectResponse[1] = apiNewReaction
    81  	assert.Len(t, apiReactions, 2)
    82  	for i, r := range apiReactions {
    83  		assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
    84  		assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
    85  		assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
    86  	}
    87  }
    88  
    89  func TestAPICommentReactions(t *testing.T) {
    90  	defer tests.PrepareTestEnv(t)()
    91  
    92  	comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
    93  	_ = comment.LoadIssue(db.DefaultContext)
    94  	issue := comment.Issue
    95  	_ = issue.LoadRepo(db.DefaultContext)
    96  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
    97  
    98  	session := loginUser(t, owner.Name)
    99  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
   100  
   101  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   102  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   103  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions", owner.Name, issue.Repo.Name, comment.ID)
   104  
   105  	// Try to add not allowed reaction
   106  	req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
   107  		Reaction: "wrong",
   108  	}).AddTokenAuth(token)
   109  	MakeRequest(t, req, http.StatusForbidden)
   110  
   111  	// Delete none existing reaction
   112  	req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
   113  		Reaction: "eyes",
   114  	}).AddTokenAuth(token)
   115  	MakeRequest(t, req, http.StatusOK)
   116  
   117  	t.Run("UnrelatedCommentID", func(t *testing.T) {
   118  		// Using the ID of a comment that does not belong to the repository must fail
   119  		repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
   120  		repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   121  		token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue)
   122  		urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions", repoOwner.Name, repo.Name, comment.ID)
   123  		req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
   124  			Reaction: "+1",
   125  		}).AddTokenAuth(token)
   126  		MakeRequest(t, req, http.StatusNotFound)
   127  		req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
   128  			Reaction: "+1",
   129  		}).AddTokenAuth(token)
   130  		MakeRequest(t, req, http.StatusNotFound)
   131  
   132  		req = NewRequest(t, "GET", urlStr).
   133  			AddTokenAuth(token)
   134  		MakeRequest(t, req, http.StatusNotFound)
   135  	})
   136  
   137  	// Add allowed reaction
   138  	req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
   139  		Reaction: "+1",
   140  	}).AddTokenAuth(token)
   141  	resp := MakeRequest(t, req, http.StatusCreated)
   142  	var apiNewReaction api.Reaction
   143  	DecodeJSON(t, resp, &apiNewReaction)
   144  
   145  	// Add existing reaction
   146  	MakeRequest(t, req, http.StatusForbidden)
   147  
   148  	// Get end result of reaction list of issue #1
   149  	req = NewRequest(t, "GET", urlStr).
   150  		AddTokenAuth(token)
   151  	resp = MakeRequest(t, req, http.StatusOK)
   152  	var apiReactions []*api.Reaction
   153  	DecodeJSON(t, resp, &apiReactions)
   154  	expectResponse := make(map[int]api.Reaction)
   155  	expectResponse[0] = api.Reaction{
   156  		User:     convert.ToUser(db.DefaultContext, user2, user2),
   157  		Reaction: "laugh",
   158  		Created:  time.Unix(1573248004, 0),
   159  	}
   160  	expectResponse[1] = api.Reaction{
   161  		User:     convert.ToUser(db.DefaultContext, user1, user1),
   162  		Reaction: "laugh",
   163  		Created:  time.Unix(1573248005, 0),
   164  	}
   165  	expectResponse[2] = apiNewReaction
   166  	assert.Len(t, apiReactions, 3)
   167  	for i, r := range apiReactions {
   168  		assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
   169  		assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
   170  		assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
   171  	}
   172  }