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 1006B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "os"
  6. "os/exec"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. )
  11. func getWhoamiOutput() (string, error) {
  12. output, err := exec.Command("whoami").Output()
  13. if err != nil {
  14. return "", err
  15. }
  16. return strings.TrimSpace(string(output)), nil
  17. }
  18. func TestCurrentUsername(t *testing.T) {
  19. user := CurrentUsername()
  20. if len(user) == 0 {
  21. t.Errorf("expected non-empty user, got: %s", user)
  22. }
  23. // Windows whoami is weird, so just skip remaining tests
  24. if runtime.GOOS == "windows" {
  25. t.Skip("skipped test because of weird whoami on Windows")
  26. }
  27. whoami, err := getWhoamiOutput()
  28. if err != nil {
  29. t.Errorf("failed to run whoami to test current user: %f", err)
  30. }
  31. user = CurrentUsername()
  32. if user != whoami {
  33. t.Errorf("expected %s as user, got: %s", whoami, user)
  34. }
  35. os.Setenv("USER", "spoofed")
  36. user = CurrentUsername()
  37. if user != whoami {
  38. t.Errorf("expected %s as user, got: %s", whoami, user)
  39. }
  40. }