1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-02-02 15:09:33 -05:00
gitea/modules/user/user_test.go

42 lines
870 B
Go
Raw Normal View History

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2016-11-06 03:47:25 -05:00
package user
import (
"os/exec"
"runtime"
"strings"
2016-11-06 03:47:25 -05:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-11-06 03:47:25 -05:00
)
func getWhoamiOutput() (string, error) {
output, err := exec.Command("whoami").Output()
if err != nil {
return "", err
}
2019-06-12 15:41:28 -04:00
return strings.TrimSpace(string(output)), nil
}
2016-11-06 03:47:25 -05:00
func TestCurrentUsername(t *testing.T) {
2016-11-06 03:47:25 -05:00
user := CurrentUsername()
require.NotEmpty(t, 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()
require.NoError(t, err)
user = CurrentUsername()
assert.Equal(t, whoami, user)
t.Setenv("USER", "spoofed")
2016-11-06 03:47:25 -05:00
user = CurrentUsername()
assert.Equal(t, whoami, user)
2016-11-06 03:47:25 -05:00
}