2017-08-02 22:09:16 -07:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2017-08-02 22:09:16 -07:00
package user
import (
"net/http"
"testing"
2023-10-15 23:46:06 +08:00
"code.gitea.io/gitea/models/db"
2024-04-04 17:57:11 +05:30
issues_model "code.gitea.io/gitea/models/issues"
2022-06-06 16:01:49 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-12 22:36:47 +08:00
"code.gitea.io/gitea/models/unittest"
2019-08-23 09:40:30 -07:00
"code.gitea.io/gitea/modules/setting"
2024-03-04 09:02:51 +08:00
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/services/context"
2024-02-27 15:12:22 +08:00
"code.gitea.io/gitea/services/contexttest"
2017-08-02 22:09:16 -07:00
"github.com/stretchr/testify/assert"
2024-07-30 19:41:10 +00:00
"github.com/stretchr/testify/require"
2017-08-02 22:09:16 -07:00
)
2021-01-13 05:19:17 +01:00
func TestArchivedIssues ( t * testing . T ) {
// Arrange
setting . UI . IssuePagingNum = 1
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2021-01-13 05:19:17 +01:00
2023-09-01 19:26:07 +08:00
ctx , _ := contexttest . MockContext ( t , "issues" )
contexttest . LoadUser ( t , ctx , 30 )
2021-01-13 05:19:17 +01:00
ctx . Req . Form . Set ( "state" , "open" )
2024-09-13 12:55:39 +00:00
ctx . Req . Form . Set ( "type" , "your_repositories" )
2021-01-13 05:19:17 +01:00
// Assume: User 30 has access to two Repos with Issues, one of the Repos being archived.
2023-10-15 23:46:06 +08:00
repos , _ , _ := repo_model . GetUserRepositories ( db . DefaultContext , & repo_model . SearchRepoOptions { Actor : ctx . Doer } )
2022-12-12 20:45:21 +08:00
assert . Len ( t , repos , 3 )
2021-01-13 05:19:17 +01:00
IsArchived := make ( map [ int64 ] bool )
NumIssues := make ( map [ int64 ] int )
for _ , repo := range repos {
IsArchived [ repo . ID ] = repo . IsArchived
NumIssues [ repo . ID ] = repo . NumIssues
}
2021-06-07 07:27:09 +02:00
assert . False ( t , IsArchived [ 50 ] )
2021-01-13 05:19:17 +01:00
assert . EqualValues ( t , 1 , NumIssues [ 50 ] )
2021-06-07 07:27:09 +02:00
assert . True ( t , IsArchived [ 51 ] )
2021-01-13 05:19:17 +01:00
assert . EqualValues ( t , 1 , NumIssues [ 51 ] )
// Act
Issues ( ctx )
// Assert: One Issue (ID 30) from one Repo (ID 50) is retrieved, while nothing from archived Repo 51 is retrieved
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
assert . Len ( t , ctx . Data [ "Issues" ] , 1 )
}
2017-08-02 22:09:16 -07:00
func TestIssues ( t * testing . T ) {
setting . UI . IssuePagingNum = 1
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2017-08-02 22:09:16 -07:00
2023-09-01 19:26:07 +08:00
ctx , _ := contexttest . MockContext ( t , "issues" )
contexttest . LoadUser ( t , ctx , 2 )
2017-08-02 22:09:16 -07:00
ctx . Req . Form . Set ( "state" , "closed" )
Issues ( ctx )
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
assert . EqualValues ( t , true , ctx . Data [ "IsShowClosed" ] )
assert . Len ( t , ctx . Data [ "Issues" ] , 1 )
}
2019-12-15 08:20:08 -06:00
2021-01-13 05:19:17 +01:00
func TestPulls ( t * testing . T ) {
setting . UI . IssuePagingNum = 20
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2021-01-13 05:19:17 +01:00
2023-09-01 19:26:07 +08:00
ctx , _ := contexttest . MockContext ( t , "pulls" )
contexttest . LoadUser ( t , ctx , 2 )
2021-01-13 05:19:17 +01:00
ctx . Req . Form . Set ( "state" , "open" )
2024-09-13 12:55:39 +00:00
ctx . Req . Form . Set ( "type" , "your_repositories" )
2021-01-13 05:19:17 +01:00
Pulls ( ctx )
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
2023-07-28 21:18:12 +02:00
assert . Len ( t , ctx . Data [ "Issues" ] , 5 )
2021-01-13 05:19:17 +01:00
}
2019-12-15 08:20:08 -06:00
func TestMilestones ( t * testing . T ) {
setting . UI . IssuePagingNum = 1
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2019-12-15 08:20:08 -06:00
2023-09-01 19:26:07 +08:00
ctx , _ := contexttest . MockContext ( t , "milestones" )
contexttest . LoadUser ( t , ctx , 2 )
2019-12-15 08:20:08 -06:00
ctx . SetParams ( "sort" , "issues" )
ctx . Req . Form . Set ( "state" , "closed" )
ctx . Req . Form . Set ( "sort" , "furthestduedate" )
Milestones ( ctx )
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
assert . EqualValues ( t , map [ int64 ] int64 { 1 : 1 } , ctx . Data [ "Counts" ] )
assert . EqualValues ( t , true , ctx . Data [ "IsShowClosed" ] )
assert . EqualValues ( t , "furthestduedate" , ctx . Data [ "SortType" ] )
assert . EqualValues ( t , 1 , ctx . Data [ "Total" ] )
assert . Len ( t , ctx . Data [ "Milestones" ] , 1 )
2020-03-31 15:47:00 +08:00
assert . Len ( t , ctx . Data [ "Repos" ] , 2 ) // both repo 42 and 1 have milestones and both are owned by user 2
2019-12-15 08:20:08 -06:00
}
func TestMilestonesForSpecificRepo ( t * testing . T ) {
setting . UI . IssuePagingNum = 1
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2019-12-15 08:20:08 -06:00
2023-09-01 19:26:07 +08:00
ctx , _ := contexttest . MockContext ( t , "milestones" )
contexttest . LoadUser ( t , ctx , 2 )
2019-12-15 08:20:08 -06:00
ctx . SetParams ( "sort" , "issues" )
ctx . SetParams ( "repo" , "1" )
ctx . Req . Form . Set ( "state" , "closed" )
ctx . Req . Form . Set ( "sort" , "furthestduedate" )
Milestones ( ctx )
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
assert . EqualValues ( t , map [ int64 ] int64 { 1 : 1 } , ctx . Data [ "Counts" ] )
assert . EqualValues ( t , true , ctx . Data [ "IsShowClosed" ] )
assert . EqualValues ( t , "furthestduedate" , ctx . Data [ "SortType" ] )
assert . EqualValues ( t , 1 , ctx . Data [ "Total" ] )
assert . Len ( t , ctx . Data [ "Milestones" ] , 1 )
2020-03-31 15:47:00 +08:00
assert . Len ( t , ctx . Data [ "Repos" ] , 2 ) // both repo 42 and 1 have milestones and both are owned by user 2
2019-12-15 08:20:08 -06:00
}
2024-03-04 09:02:51 +08:00
func TestDashboardPagination ( t * testing . T ) {
ctx , _ := contexttest . MockContext ( t , "/" , contexttest . MockContextOption { Render : templates . HTMLRenderer ( ) } )
page := context . NewPagination ( 10 , 3 , 1 , 3 )
setting . AppSubURL = "/SubPath"
out , err := ctx . RenderToHTML ( "base/paginate" , map [ string ] any { "Link" : setting . AppSubURL , "Page" : page } )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2024-03-04 09:02:51 +08:00
assert . Contains ( t , out , ` <a class=" item navigation" href="/SubPath/?page=2"> ` )
setting . AppSubURL = ""
out , err = ctx . RenderToHTML ( "base/paginate" , map [ string ] any { "Link" : setting . AppSubURL , "Page" : page } )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2024-03-04 09:02:51 +08:00
assert . Contains ( t , out , ` <a class=" item navigation" href="/?page=2"> ` )
}
2024-04-04 17:57:11 +05:30
func TestOrgLabels ( t * testing . T ) {
2024-07-30 19:41:10 +00:00
require . NoError ( t , unittest . LoadFixtures ( ) )
2024-04-04 17:57:11 +05:30
ctx , _ := contexttest . MockContext ( t , "org/org3/issues" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadOrganization ( t , ctx , 3 )
Issues ( ctx )
assert . EqualValues ( t , http . StatusOK , ctx . Resp . Status ( ) )
assert . True ( t , ctx . Data [ "PageIsOrgIssues" ] . ( bool ) )
orgLabels := [ ] struct {
ID int64
OrgID int64
Name string
} {
{ 3 , 3 , "orglabel3" } ,
{ 4 , 3 , "orglabel4" } ,
}
labels , ok := ctx . Data [ "Labels" ] . ( [ ] * issues_model . Label )
assert . True ( t , ok )
if assert . Len ( t , labels , len ( orgLabels ) ) {
for i , label := range labels {
assert . EqualValues ( t , orgLabels [ i ] . OrgID , label . OrgID )
assert . EqualValues ( t , orgLabels [ i ] . ID , label . ID )
assert . EqualValues ( t , orgLabels [ i ] . Name , label . Name )
}
}
}