|
| 1 | +package ldap |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestConn_Del(t *testing.T) { |
| 10 | + l, err := getTestConnection(true) |
| 11 | + if err != nil { |
| 12 | + t.Fatal(err) |
| 13 | + } |
| 14 | + defer l.Close() |
| 15 | + |
| 16 | + dn := "cn=testuser,ou=people,dc=example,dc=com" |
| 17 | + |
| 18 | + // Remove the entry if it exists from previous test runs |
| 19 | + _ = l.Del(NewDelRequest(dn, nil)) |
| 20 | + |
| 21 | + assert.NoError(t, l.Add(&AddRequest{ |
| 22 | + DN: dn, |
| 23 | + Attributes: []Attribute{ |
| 24 | + { |
| 25 | + Type: "objectClass", |
| 26 | + Vals: []string{"top", "person", "organizationalPerson", "inetOrgPerson"}, |
| 27 | + }, |
| 28 | + { |
| 29 | + Type: "cn", |
| 30 | + Vals: []string{"testuser"}, |
| 31 | + }, |
| 32 | + { |
| 33 | + Type: "givenName", |
| 34 | + Vals: []string{"Test User"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + Type: "sn", |
| 38 | + Vals: []string{"Dummy"}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + })) |
| 42 | + t.Logf("Added user") |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + dn string |
| 47 | + wantErr bool |
| 48 | + errorCode uint16 |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "empty DN", |
| 52 | + dn: "", |
| 53 | + wantErr: true, |
| 54 | + errorCode: LDAPResultUnwillingToPerform, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "invalid DN", |
| 58 | + dn: "AAAAAAAAAAAAAAAAAA", |
| 59 | + wantErr: true, |
| 60 | + errorCode: LDAPResultInvalidDNSyntax, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "delete user", |
| 64 | + dn: dn, |
| 65 | + wantErr: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "delete entry with children", |
| 69 | + dn: "ou=people," + baseDN, |
| 70 | + wantErr: true, |
| 71 | + errorCode: LDAPResultNotAllowedOnNonLeaf, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "delete non existing entry", |
| 75 | + dn: "ou=nonexisting," + baseDN, |
| 76 | + wantErr: true, |
| 77 | + errorCode: LDAPResultNoSuchObject, |
| 78 | + }, |
| 79 | + } |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + delRequest := NewDelRequest(tt.dn, nil) |
| 83 | + err := l.Del(delRequest) |
| 84 | + if tt.wantErr && err != nil { |
| 85 | + assert.Error(t, err) |
| 86 | + assert.Truef(t, IsErrorWithCode(err, tt.errorCode), "Expected error with code %d, got %d", tt.errorCode, err) |
| 87 | + } else { |
| 88 | + assert.NoError(t, err) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + t.Run("nil DelRequest", func(t *testing.T) { |
| 94 | + err := l.Del(nil) |
| 95 | + assert.Error(t, err) |
| 96 | + }) |
| 97 | +} |
0 commit comments