summaryrefslogtreecommitdiffstats
path: root/modules/user/user_test.go
blob: 372a675d34279e5291e9a6346635826d1b4828ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package user

import (
	"os/exec"
	"runtime"
	"strings"
	"testing"
)

func getWhoamiOutput() (string, error) {
	output, err := exec.Command("whoami").Output()
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(string(output)), nil
}

func TestCurrentUsername(t *testing.T) {
	user := CurrentUsername()
	if len(user) == 0 {
		t.Errorf("expected non-empty user, got: %s", user)
	}
	// Windows whoami is weird, so just skip remaining tests
	if runtime.GOOS == "windows" {
		t.Skip("skipped test because of weird whoami on Windows")
	}
	whoami, err := getWhoamiOutput()
	if err != nil {
		t.Errorf("failed to run whoami to test current user: %f", err)
	}
	user = CurrentUsername()
	if user != whoami {
		t.Errorf("expected %s as user, got: %s", whoami, user)
	}
	t.Setenv("USER", "spoofed")
	user = CurrentUsername()
	if user != whoami {
		t.Errorf("expected %s as user, got: %s", whoami, user)
	}
}