gitea源码

user_test.go 870B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "os/exec"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func getWhoamiOutput() (string, error) {
  13. output, err := exec.Command("whoami").Output()
  14. if err != nil {
  15. return "", err
  16. }
  17. return strings.TrimSpace(string(output)), nil
  18. }
  19. func TestCurrentUsername(t *testing.T) {
  20. user := CurrentUsername()
  21. require.NotEmpty(t, user)
  22. // Windows whoami is weird, so just skip remaining tests
  23. if runtime.GOOS == "windows" {
  24. t.Skip("skipped test because of weird whoami on Windows")
  25. }
  26. whoami, err := getWhoamiOutput()
  27. require.NoError(t, err)
  28. user = CurrentUsername()
  29. assert.Equal(t, whoami, user)
  30. t.Setenv("USER", "spoofed")
  31. user = CurrentUsername()
  32. assert.Equal(t, whoami, user)
  33. }