You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_test.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "os"
  7. "os/exec"
  8. "runtime"
  9. "strings"
  10. "testing"
  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. if len(user) == 0 {
  22. t.Errorf("expected non-empty user, got: %s", user)
  23. }
  24. // Windows whoami is weird, so just skip remaining tests
  25. if runtime.GOOS == "windows" {
  26. t.Skip("skipped test because of weird whoami on Windows")
  27. }
  28. whoami, err := getWhoamiOutput()
  29. if err != nil {
  30. t.Errorf("failed to run whoami to test current user: %f", err)
  31. }
  32. user = CurrentUsername()
  33. if user != whoami {
  34. t.Errorf("expected %s as user, got: %s", whoami, user)
  35. }
  36. os.Setenv("USER", "spoofed")
  37. user = CurrentUsername()
  38. if user != whoami {
  39. t.Errorf("expected %s as user, got: %s", whoami, user)
  40. }
  41. }