2020-10-17 09:53:08 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-06-27 01:23:14 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-05-12 09:43:42 +05:30
|
|
|
|
2020-10-17 09:53:08 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_ToUser(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-10-17 09:53:08 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User)
|
2020-10-17 09:53:08 +05:30
|
|
|
|
2021-03-27 22:15:26 +05:30
|
|
|
apiUser := toUser(user1, true, true)
|
2020-10-17 09:53:08 +05:30
|
|
|
assert.True(t, apiUser.IsAdmin)
|
2021-10-25 10:31:16 +05:30
|
|
|
assert.Contains(t, apiUser.AvatarURL, "://")
|
2020-10-17 09:53:08 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User)
|
2020-10-17 09:53:08 +05:30
|
|
|
|
2021-03-27 22:15:26 +05:30
|
|
|
apiUser = toUser(user2, true, true)
|
2020-10-17 09:53:08 +05:30
|
|
|
assert.False(t, apiUser.IsAdmin)
|
|
|
|
|
2021-03-27 22:15:26 +05:30
|
|
|
apiUser = toUser(user1, false, false)
|
2020-10-17 09:53:08 +05:30
|
|
|
assert.False(t, apiUser.IsAdmin)
|
2021-06-27 01:23:14 +05:30
|
|
|
assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility)
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
user31 := db.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User)
|
2021-06-27 01:23:14 +05:30
|
|
|
|
|
|
|
apiUser = toUser(user31, true, true)
|
|
|
|
assert.False(t, apiUser.IsAdmin)
|
|
|
|
assert.EqualValues(t, api.VisibleTypePrivate.String(), apiUser.Visibility)
|
2020-10-17 09:53:08 +05:30
|
|
|
}
|