Rename root, api and sdk module paths (imports, go.mod, go_package, ldflags, build scripts, buf configs) and regenerate protobuf/gateway code with the project's pinned toolchain. Wire identifiers unchanged. api gets a local replace for sdk; root/api/sdk require v0.1.0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
153 lines
3.8 KiB
Go
153 lines
3.8 KiB
Go
// Copyright IBM Corp. 2020, 2026
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package ldap
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/auth/ldap/store"
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestNewUrl(t *testing.T) {
|
|
t.Parallel()
|
|
testCtx := context.Background()
|
|
tests := []struct {
|
|
name string
|
|
ctx context.Context
|
|
authMethodId string
|
|
priority int
|
|
url *url.URL
|
|
want *Url
|
|
wantErr bool
|
|
wantErrCode errors.Code
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "valid",
|
|
ctx: testCtx,
|
|
authMethodId: "test-id",
|
|
priority: 10,
|
|
url: TestConvertToUrls(t, "ldaps://alice.com")[0],
|
|
want: &Url{
|
|
Url: &store.Url{
|
|
LdapMethodId: "test-id",
|
|
ConnectionPriority: 10,
|
|
ServerUrl: "ldaps://alice.com",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "missing-auth-method-id",
|
|
ctx: testCtx,
|
|
url: TestConvertToUrls(t, "ldaps://alice.com")[0],
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrContains: "missing auth method id",
|
|
},
|
|
{
|
|
name: "missing-url",
|
|
ctx: testCtx,
|
|
priority: 1,
|
|
authMethodId: "test-id",
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrContains: "missing url",
|
|
},
|
|
{
|
|
name: "invalid-scheme",
|
|
ctx: testCtx,
|
|
authMethodId: "test-id",
|
|
priority: 1,
|
|
url: func() *url.URL {
|
|
parsed, err := url.Parse("https://alice.com")
|
|
require.NoError(t, err)
|
|
return parsed
|
|
}(),
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrContains: "scheme \"https\" is not ldap or ldaps",
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
got, err := NewUrl(tc.ctx, tc.authMethodId, tc.priority, tc.url)
|
|
if tc.wantErr {
|
|
require.Error(err)
|
|
assert.Nil(got)
|
|
if tc.wantErrCode != errors.Unknown {
|
|
assert.True(errors.Match(errors.T(tc.wantErrCode), err))
|
|
}
|
|
if tc.wantErrContains != "" {
|
|
assert.Contains(err.Error(), tc.wantErrContains)
|
|
}
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
assert.Equal(tc.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUrl_SetTableName(t *testing.T) {
|
|
t.Parallel()
|
|
defaultTableName := urlTableName
|
|
tests := []struct {
|
|
name string
|
|
setNameTo string
|
|
want string
|
|
}{
|
|
{
|
|
name: "new-name",
|
|
setNameTo: "new-name",
|
|
want: "new-name",
|
|
},
|
|
{
|
|
name: "reset to default",
|
|
setNameTo: "",
|
|
want: defaultTableName,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
def := allocUrl()
|
|
require.Equal(defaultTableName, def.TableName())
|
|
m := allocUrl()
|
|
m.SetTableName(tt.setNameTo)
|
|
assert.Equal(tt.want, m.TableName())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUrl_clone(t *testing.T) {
|
|
t.Parallel()
|
|
testCtx := context.Background()
|
|
const priorityOfOne = 1
|
|
t.Run("valid", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
u, err := NewUrl(testCtx, "test-id", priorityOfOne, TestConvertToUrls(t, "ldaps://alice.com")[0])
|
|
require.NoError(err)
|
|
cp := u.clone()
|
|
assert.True(proto.Equal(cp.Url, u.Url))
|
|
})
|
|
t.Run("not-equal", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
u, err := NewUrl(testCtx, "test-id", priorityOfOne, TestConvertToUrls(t, "ldaps://alice.com")[0])
|
|
require.NoError(err)
|
|
|
|
u2, err := NewUrl(testCtx, "test-id", priorityOfOne, TestConvertToUrls(t, "ldaps://bob.com")[0])
|
|
require.NoError(err)
|
|
|
|
cp := u.clone()
|
|
assert.True(!proto.Equal(cp.Url, u2.Url))
|
|
})
|
|
}
|