Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify PAM service tests #35

Merged
merged 2 commits into from
Sep 15, 2023
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
67 changes: 38 additions & 29 deletions internal/services/pam/pam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ var (
}
)

func TestNewService(t *testing.T) {
t.Parallel()

service := pam.NewService(context.Background(), brokerManager)

brokers, err := service.AvailableBrokers(context.Background(), &authd.Empty{})
require.NoError(t, err, "can’t create the service directly")
require.NotEmpty(t, brokers.BrokersInfos, "Service is created and can query the broker manager")
}

func TestAvailableBrokers(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -153,8 +163,7 @@ func TestGetAuthenticationModes(t *testing.T) {
supportedUILayouts []*authd.UILayout

// These are auxiliary inputs that affect the test setup and help control the mock output.
username string
noSession bool
username string

// This is the expected return.
wantErr bool
Expand All @@ -164,7 +173,7 @@ func TestGetAuthenticationModes(t *testing.T) {

"Error when sessionID is empty": {sessionID: "-", wantErr: true},
"Error when passing invalid layout": {supportedUILayouts: []*authd.UILayout{emptyType}, wantErr: true},
"Error when broker does not exist": {sessionID: "no broker", noSession: true, wantErr: true},
"Error when sessionID is invalid": {sessionID: "invalid-session", wantErr: true},
"Error when getting authentication modes": {username: "GAM_error", wantErr: true},
"Error when broker returns invalid modes": {username: "GAM_invalid", wantErr: true},
}
Expand All @@ -175,15 +184,16 @@ func TestGetAuthenticationModes(t *testing.T) {

client := newPamClient(t)

if !tc.noSession {
switch tc.sessionID {
case "invalid-session":
case "-":
tc.sessionID = ""
default:
id := startSession(t, client, tc.username)
if tc.sessionID == "" {
tc.sessionID = id
}
}
if tc.sessionID == "-" {
tc.sessionID = ""
}

if tc.supportedUILayouts == nil {
tc.supportedUILayouts = []*authd.UILayout{requiredEntry}
Expand Down Expand Up @@ -219,7 +229,6 @@ func TestSelectAuthenticationMode(t *testing.T) {
username string
supportedUILayouts []*authd.UILayout
noValidators bool
noSession bool

// This is the expected return.
wantErr bool
Expand All @@ -228,8 +237,8 @@ func TestSelectAuthenticationMode(t *testing.T) {
"Successfully select mode with missing optional value": {username: "SAM_missing_optional_entry", supportedUILayouts: []*authd.UILayout{optionalEntry}},

// service errors
"Error when broker does not exist": {sessionID: "no broker", noSession: true, wantErr: true},
"Error when sessionID is empty": {sessionID: "-", wantErr: true},
"Error when session ID is invalid": {sessionID: "invalid-session", wantErr: true},
"Error when no authmode is selected": {sessionID: "no auth mode", authMode: "-", wantErr: true},

// broker errors
Expand All @@ -248,15 +257,16 @@ func TestSelectAuthenticationMode(t *testing.T) {

client := newPamClient(t)

if !tc.noSession {
switch tc.sessionID {
case "invalid-session":
case "-":
tc.sessionID = ""
default:
id := startSession(t, client, tc.username)
if tc.sessionID == "" {
tc.sessionID = id
}
}
if tc.sessionID == "-" {
tc.sessionID = ""
}

if tc.authMode == "" {
tc.authMode = "some mode"
Expand Down Expand Up @@ -302,7 +312,6 @@ func TestIsAuthenticated(t *testing.T) {

// These are auxiliary inputs that affect the test setup and help control the mock output.
username string
noSession bool
secondCall bool
cancelFirstCall bool
}{
Expand All @@ -314,7 +323,7 @@ func TestIsAuthenticated(t *testing.T) {

// service errors
"Error when sessionID is empty": {sessionID: "-"},
"Error when there is no broker": {sessionID: "no broker", noSession: true},
"Error when there is no broker": {sessionID: "invalid-session"},

// broker errors
"Error when authenticating": {username: "IA_error"},
Expand All @@ -329,15 +338,16 @@ func TestIsAuthenticated(t *testing.T) {

client := newPamClient(t)

if !tc.noSession {
switch tc.sessionID {
case "invalid-session":
case "-":
tc.sessionID = ""
default:
id := startSession(t, client, tc.username)
if tc.sessionID == "" {
tc.sessionID = id
}
}
if tc.sessionID == "-" {
tc.sessionID = ""
}

var firstCall, secondCall string
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -444,18 +454,16 @@ func TestEndSession(t *testing.T) {
sessionID string

// These are auxiliary inputs that affect the test setup and help control the mock output.
username string
noSession bool
username string

// This is the expected return.
wantErr bool
}{
"Successfully end session": {username: "success"},

"Error when sessionID is empty": {sessionID: "-", wantErr: true},
"Error when sessionID is invalid": {sessionID: "invalid", wantErr: true},
"Error when broker does not exist": {sessionID: "no broker", noSession: true, wantErr: true},
"Error when ending session": {username: "ES_error", wantErr: true},
"Error when sessionID is empty": {sessionID: "-", wantErr: true},
"Error when sessionID is invalid": {sessionID: "invalid-session", wantErr: true},
"Error when ending session": {username: "ES_error", wantErr: true},
}
for name, tc := range tests {
tc := tc
Expand All @@ -464,15 +472,16 @@ func TestEndSession(t *testing.T) {

client := newPamClient(t)

if !tc.noSession {
switch tc.sessionID {
case "invalid-session":
case "-":
tc.sessionID = ""
default:
id := startSession(t, client, tc.username)
if tc.sessionID == "" {
tc.sessionID = id
}
}
if tc.sessionID == "-" {
tc.sessionID = ""
}

esReq := &authd.ESRequest{
SessionId: tc.sessionID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authentication: no broker found for session "no broker"
err: rpc error: code = Unknown desc = can't check authentication: no broker found for session "invalid-session"