@@ -10,78 +10,77 @@ import (
10
10
"io"
11
11
"mime/multipart"
12
12
"net/http"
13
- "net/url"
14
13
"testing"
15
14
16
15
"code.gitea.io/gitea/models/db"
17
16
"code.gitea.io/gitea/models/unittest"
18
17
user_model "code.gitea.io/gitea/models/user"
19
18
"code.gitea.io/gitea/modules/avatar"
19
+ "code.gitea.io/gitea/tests"
20
20
21
21
"github.com/stretchr/testify/assert"
22
22
)
23
23
24
24
func TestUserAvatar (t * testing.T ) {
25
- onGiteaRun ( t , func ( t * testing. T , u * url. URL ) {
26
- user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 }) // owner of the repo3, is an org
25
+ defer tests . PrepareTestEnv ( t )()
26
+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 }) // owner of the repo3, is an org
27
27
28
- seed := user2 .Email
29
- if len (seed ) == 0 {
30
- seed = user2 .Name
31
- }
28
+ seed := user2 .Email
29
+ if len (seed ) == 0 {
30
+ seed = user2 .Name
31
+ }
32
32
33
- img , err := avatar .RandomImage ([]byte (seed ))
34
- if err != nil {
35
- assert .NoError (t , err )
36
- return
37
- }
33
+ img , err := avatar .RandomImage ([]byte (seed ))
34
+ if err != nil {
35
+ assert .NoError (t , err )
36
+ return
37
+ }
38
38
39
- session := loginUser (t , "user2" )
40
- csrf := GetUserCSRFToken (t , session )
39
+ session := loginUser (t , "user2" )
40
+ csrf := GetUserCSRFToken (t , session )
41
41
42
- imgData := & bytes.Buffer {}
42
+ imgData := & bytes.Buffer {}
43
43
44
- body := & bytes.Buffer {}
44
+ body := & bytes.Buffer {}
45
45
46
- // Setup multi-part
47
- writer := multipart .NewWriter (body )
48
- writer .WriteField ("source" , "local" )
49
- part , err := writer .CreateFormFile ("avatar" , "avatar-for-testuseravatar.png" )
50
- if err != nil {
51
- assert .NoError (t , err )
52
- return
53
- }
46
+ // Setup multi-part
47
+ writer := multipart .NewWriter (body )
48
+ writer .WriteField ("source" , "local" )
49
+ part , err := writer .CreateFormFile ("avatar" , "avatar-for-testuseravatar.png" )
50
+ if err != nil {
51
+ assert .NoError (t , err )
52
+ return
53
+ }
54
54
55
- if err := png .Encode (imgData , img ); err != nil {
56
- assert .NoError (t , err )
57
- return
58
- }
55
+ if err := png .Encode (imgData , img ); err != nil {
56
+ assert .NoError (t , err )
57
+ return
58
+ }
59
59
60
- if _ , err := io .Copy (part , imgData ); err != nil {
61
- assert .NoError (t , err )
62
- return
63
- }
60
+ if _ , err := io .Copy (part , imgData ); err != nil {
61
+ assert .NoError (t , err )
62
+ return
63
+ }
64
64
65
- if err := writer .Close (); err != nil {
66
- assert .NoError (t , err )
67
- return
68
- }
65
+ if err := writer .Close (); err != nil {
66
+ assert .NoError (t , err )
67
+ return
68
+ }
69
69
70
- req := NewRequestWithBody (t , "POST" , "/user/settings/avatar" , body )
71
- req .Header .Add ("X-Csrf-Token" , csrf )
72
- req .Header .Add ("Content-Type" , writer .FormDataContentType ())
70
+ req := NewRequestWithBody (t , "POST" , "/user/settings/avatar" , body )
71
+ req .Header .Add ("X-Csrf-Token" , csrf )
72
+ req .Header .Add ("Content-Type" , writer .FormDataContentType ())
73
73
74
- session .MakeRequest (t , req , http .StatusSeeOther )
74
+ session .MakeRequest (t , req , http .StatusSeeOther )
75
75
76
- user2 = unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 }) // owner of the repo3, is an org
76
+ user2 = unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 }) // owner of the repo3, is an org
77
77
78
- req = NewRequest (t , "GET" , user2 .AvatarLinkWithSize (db .DefaultContext , 0 ))
79
- _ = session .MakeRequest (t , req , http .StatusOK )
78
+ req = NewRequest (t , "GET" , user2 .AvatarLinkWithSize (db .DefaultContext , 0 ))
79
+ _ = session .MakeRequest (t , req , http .StatusOK )
80
80
81
- testGetAvatarRedirect (t , user2 )
81
+ testGetAvatarRedirect (t , user2 )
82
82
83
- // Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.
84
- })
83
+ // Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.
85
84
}
86
85
87
86
func testGetAvatarRedirect (t * testing.T , user * user_model.User ) {
0 commit comments