Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
audibleblink committed Sep 26, 2020
1 parent 4126f95 commit 0194eb2
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 42 deletions.
6 changes: 2 additions & 4 deletions pkg/attribute_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package ldsview_test
package ldsview

import (
"testing"

ldsview "github.com/kgoins/ldsview/pkg"
)

func TestAttribute_BuildFromValidLine(t *testing.T) {
attrLine := "userAccountControl: 66048"

attr, err := ldsview.BuildAttributeFromLine(attrLine)
attr, err := BuildAttributeFromLine(attrLine)
if err != nil {
t.Fatalf("Unable to build from valid attr line")
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package ldsview
import (
"strings"
"testing"

ldsview "github.com/kgoins/ldsview/pkg"
)

var EntityStr string = `# MYUSR, ContosoUsers, contoso.com
var EntityStr = `
# MYUSR, ContosoUsers, contoso.com
dn: CN=MYUSR,OU=ContosoUsers,DC=contoso,DC=com
objectClass: top
objectClass: person
Expand Down Expand Up @@ -44,12 +43,13 @@ dSCorePropagationData: 20190407190910.0Z
dSCorePropagationData: 20190311163932.0Z
dSCorePropagationData: 16010714223649.0Z
lastLogonTimestamp: 130674899604502606
`

func TestEntity_BuildEntity(t *testing.T) {
entityLines := strings.Split(EntityStr, "\n")[1:]

entity := ldsview.BuildEntity(entityLines)
entity := BuildEntity(entityLines)

if entity.IsEmpty() {
t.Fatalf("failed to parse entity")
Expand All @@ -73,9 +73,9 @@ func TestEntity_BuildEntityWithAttrFilter(t *testing.T) {
"userPrincipalName",
"objectClass",
}
attrFilter := ldsview.BuildAttributeFilter(attrsList)
attrFilter := BuildAttributeFilter(attrsList)

entity := ldsview.BuildEntityFromAttrList(entityLines, &attrFilter)
entity := BuildEntityFromAttrList(entityLines, &attrFilter)

if entity.IsEmpty() {
t.Fatalf("failed to parse entity")
Expand Down
44 changes: 19 additions & 25 deletions pkg/ldif_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
package ldsview_test
package ldsview

import (
"testing"

ldsview "github.com/kgoins/ldsview/pkg"
"github.com/stretchr/testify/assert"
)

func TestLdifParser_CountEntities(t *testing.T) {
parser := ldsview.NewLdifParser(TESTFILE)
var parser = NewLdifParser(TESTFILE)

count, err := parser.CountEntities()
if err != nil {
t.Fatalf("unable to parse test file")
}
func TestLdifParser(t *testing.T) {

if count != 5 {
t.Fatalf("Failed to count entities")
}
}

func TestLdifParser_CountEntitiesWithFilter(t *testing.T) {
parser := ldsview.NewLdifParser(TESTFILE)
t.Run("counts the ldif objects correctly", func(t *testing.T) {
want := 2
got, err := parser.CountEntities()
assert.Equal(t, want, got)
assert.Nil(t, err)
})

entityFilterStr := []string{"objectClass:=computer"}
entityFilter, _ := ldsview.BuildEntityFilter(entityFilterStr)
parser.SetEntityFilter(entityFilter)
t.Run("counts the filtered ldif objects correctly", func(t *testing.T) {
entityFilterStr := []string{"objectClass:=computer"}
entityFilter, _ := BuildEntityFilter(entityFilterStr)
parser.SetEntityFilter(entityFilter)

count, err := parser.CountEntities()
if err != nil {
t.Fatalf("unable to parse test file")
}
want := 1
got, err := parser.CountEntities()
assert.Equal(t, want, got)
assert.Nil(t, err)
})

if count != 1 {
t.Fatalf("Failed to count filtered entities")
}
}
4 changes: 2 additions & 2 deletions pkg/pkg_main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package ldsview_test
package ldsview

const TESTFILE string = "../testdata/test_users.ldif"
const TESTFILE = "../testdata/test_users.ldif"
8 changes: 3 additions & 5 deletions pkg/structure_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package ldsview_test
package ldsview

import (
"testing"

ldsview "github.com/kgoins/ldsview/pkg"
)

func TestStructureBuilder_GetStructure(t *testing.T) {
parser := ldsview.NewLdifParser(TESTFILE)
parser := NewLdifParser(TESTFILE)

structure, err := ldsview.GetStructure(&parser)
structure, err := GetStructure(&parser)
if err != nil {
t.Fatalf("failed to parse ldif for domain structure")
}
Expand Down
85 changes: 85 additions & 0 deletions testdata/test_users.ldif
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# extended LDIF
#
# LDAPv3
# base <DC=contoso,DC=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
# with pagedResults control: size=5000
#

# MYUSR, ContosoUsers, contoso.com
dn: CN=MYUSR,OU=ContosoUsers,DC=contoso,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: MYUSR
givenName: MYUSR
distinguishedName: CN=MYUSR,OU=ContosoUsers,DC=contoso,DC=com
instanceType: 4
whenCreated: 20120423175240.0Z
whenChanged: 20190225044802.0Z
displayName: MYUSR
uSNCreated: 793245
memberOf: CN=vault_users,OU=Global,OU=Security,OU=Groups,DC=contoso,DC=com
memberOf: CN=PWD Complexity,OU=Security,OU=Groups,DC=contoso,DC=com
uSNChanged: 1076364863
name: MYUSR
objectGUID:: 7OBfD10nQkSVYY8UHCV2aQ==
userAccountControl: 66048
codePage: 0
countryCode: 0
pwdLastSet: 129857191591306845
primaryGroupID: 805306368
objectSid:: AQUAAAAAAAUVAAAAa9ZiBBbA6jKDPStVYiIMAA==
accountExpires: 9223372036854775807
sAMAccountName: MYUSR
sAMAccountType: 805306368
userPrincipalName: MYUSR@contoso.com
lockoutTime: 0
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=contoso,DC=com
dSCorePropagationData: 20190827201429.0Z
dSCorePropagationData: 20190529140155.0Z
dSCorePropagationData: 20190407190910.0Z
dSCorePropagationData: 20190311163932.0Z
dSCorePropagationData: 16010714223649.0Z
lastLogonTimestamp: 130674899604502606

# MYPC, ContosoUsers, contoso.com
dn: CN=MYPC,OU=ContosoUsers,DC=contoso,DC=com
objectClass: top
objectClass: computer
objectClass: organizationalPerson
objectClass: user
cn: MYPC
givenName: MYPC
distinguishedName: CN=MYPC,OU=ContosoUsers,DC=contoso,DC=com
instanceType: 4
whenCreated: 20120423175240.0Z
whenChanged: 20190225044802.0Z
displayName: MYPC
uSNCreated: 793245
memberOf: CN=vault_users,OU=Global,OU=Security,OU=Groups,DC=contoso,DC=com
memberOf: CN=PWD Complexity,OU=Security,OU=Groups,DC=contoso,DC=com
uSNChanged: 1076364863
name: MYPC
objectGUID:: 7OBfD10nQkSVYY8UHCV2aQ==
userAccountControl: 66048
codePage: 0
countryCode: 0
pwdLastSet: 129857191591306845
primaryGroupID: 805306368
objectSid:: AQUAAAAAAAUVAAAAa9ZiBBbA6jKDPStVYiIMAA==
accountExpires: 9223372036854775807
sAMAccountName: MYPC
sAMAccountType: 805306368
userPrincipalName: MYPC@contoso.com
lockoutTime: 0
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=contoso,DC=com
dSCorePropagationData: 20190827201429.0Z
dSCorePropagationData: 20190529140155.0Z
dSCorePropagationData: 20190407190910.0Z
dSCorePropagationData: 20190311163932.0Z
dSCorePropagationData: 16010714223649.0Z
lastLogonTimestamp: 130674899604502606

0 comments on commit 0194eb2

Please sign in to comment.