code.gitea.io/gitea@v1.21.7/tests/integration/api_user_info_test.go (about)

     1  // Copyright 2022 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  
    11  	auth_model "code.gitea.io/gitea/models/auth"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	api "code.gitea.io/gitea/modules/structs"
    15  	"code.gitea.io/gitea/tests"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestAPIUserInfo(t *testing.T) {
    21  	defer tests.PrepareTestEnv(t)()
    22  
    23  	user := "user1"
    24  	user2 := "user31"
    25  
    26  	org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
    27  
    28  	session := loginUser(t, user)
    29  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
    30  
    31  	t.Run("GetInfo", func(t *testing.T) {
    32  		defer tests.PrintCurrentTest(t)()
    33  
    34  		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s?token=%s", user2, token))
    35  		resp := MakeRequest(t, req, http.StatusOK)
    36  
    37  		var u api.User
    38  		DecodeJSON(t, resp, &u)
    39  		assert.Equal(t, user2, u.UserName)
    40  
    41  		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user2))
    42  		MakeRequest(t, req, http.StatusNotFound)
    43  
    44  		// test if the placaholder Mail is returned if a User is not logged in
    45  		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", org3.Name))
    46  		resp = MakeRequest(t, req, http.StatusOK)
    47  		DecodeJSON(t, resp, &u)
    48  		assert.Equal(t, org3.GetPlaceholderEmail(), u.Email)
    49  
    50  		// Test if the correct Mail is returned if a User is logged in
    51  		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s?token=%s", org3.Name, token))
    52  		resp = MakeRequest(t, req, http.StatusOK)
    53  		DecodeJSON(t, resp, &u)
    54  		assert.Equal(t, org3.GetEmail(), u.Email)
    55  	})
    56  
    57  	t.Run("GetAuthenticatedUser", func(t *testing.T) {
    58  		defer tests.PrintCurrentTest(t)()
    59  
    60  		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user?token=%s", token))
    61  		resp := MakeRequest(t, req, http.StatusOK)
    62  
    63  		var u api.User
    64  		DecodeJSON(t, resp, &u)
    65  		assert.Equal(t, user, u.UserName)
    66  	})
    67  }