Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions commands/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,19 @@
package commands

import (
"os/user"
"testing"

"github.com/openpubkey/opkssh/policy"
"github.com/openpubkey/opkssh/policy/files"
"github.com/openpubkey/opkssh/test/testutil"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

// Duplicates code from multipolicyloader_test.go
type MockUserLookup struct {
// User is returned on any call to Lookup() if Error is nil
User *user.User
// Error is returned on any call to Lookup() if non-nil
Error error
}

// Lookup implements policy.UserLookup
func (m *MockUserLookup) Lookup(username string) (*user.User, error) {
if m.Error == nil {
return m.User, nil
} else {
return nil, m.Error
}
}
// MockUserLookup is an alias for testutil.MockUserLookup to keep test code concise.
type MockUserLookup = testutil.MockUserLookup

var ValidUser *user.User = &user.User{HomeDir: "/home/foo", Username: "foo"}
var ValidUser = testutil.ValidUser

func MockAddCmd(mockFs afero.Fs) *AddCmd {
mockUserLookup := &MockUserLookup{User: ValidUser}
Expand Down
22 changes: 4 additions & 18 deletions policy/policyloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,13 @@ import (

"github.com/openpubkey/opkssh/policy"
"github.com/openpubkey/opkssh/policy/files"
"github.com/openpubkey/opkssh/test/testutil"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

type MockUserLookup struct {
// User is returned on any call to Lookup() if Error is nil
User *user.User
// Error is returned on any call to Lookup() if non-nil
Error error
}

var _ policy.UserLookup = &MockUserLookup{}

// Lookup implements policy.UserLookup
func (m *MockUserLookup) Lookup(username string) (*user.User, error) {
if m.Error == nil {
return m.User, nil
} else {
return nil, m.Error
}
}
// MockUserLookup is an alias for testutil.MockUserLookup to keep test code concise.
type MockUserLookup = testutil.MockUserLookup

// MockFsOpenError embeds an afero.MemMapFs (implements afero.Fs) but allows for
// finer control on when an error should be returned on a specific filepath
Expand Down Expand Up @@ -96,7 +82,7 @@ func NewTestSystemPolicyLoader(fs afero.Fs, userLookup policy.UserLookup) *polic
}
}

var ValidUser *user.User = &user.User{HomeDir: "/home/foo", Username: "foo"}
var ValidUser = testutil.ValidUser

func TestLoadUserPolicy_FailUserLookup(t *testing.T) {
// Test that LoadUserPolicy returns an error when user lookup fails
Expand Down
50 changes: 50 additions & 0 deletions test/testutil/mockuserlookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2026 OpenPubkey
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package testutil

import (
"fmt"
"os/user"

"github.com/openpubkey/opkssh/policy"
)

// ValidUser is a shared test fixture representing a valid OS user.
var ValidUser = &user.User{HomeDir: "/home/foo", Username: "foo"}

// MockUserLookup implements [policy.UserLookup] for testing.
// - Set [User] for a default user returned on any Lookup call.
// - Set [Error] to force every Lookup call to fail.
type MockUserLookup struct {
// User is returned on any call to Lookup() if Error is nil.
User *user.User
// Error, if non-nil, is returned on any call to Lookup().
Error error
}

var _ policy.UserLookup = &MockUserLookup{}

// Lookup implements [policy.UserLookup].
func (m *MockUserLookup) Lookup(username string) (*user.User, error) {
if m.Error != nil {
return nil, m.Error
}
if m.User != nil {
return m.User, nil
}
return nil, fmt.Errorf("user %q not found", username)
}
Loading