1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-19 14:14:26 +00: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 09:47:25 +01:00
package user
import (
"os/exec"
"runtime"
"strings"
2016-11-06 09:47:25 +01:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-11-06 09:47:25 +01:00
)
func getWhoamiOutput() (string, error) {
output, err := exec.Command("whoami").Output()
if err != nil {
return "", err
}
2019-06-12 21:41:28 +02:00
return strings.TrimSpace(string(output)), nil
}
2016-11-06 09:47:25 +01:00
func TestCurrentUsername(t *testing.T) {
2016-11-06 09:47:25 +01: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 09:47:25 +01:00
user = CurrentUsername()
assert.Equal(t, whoami, user)
2016-11-06 09:47:25 +01:00
}