ldap-client/test/Ldap/Client/ModifySpec.hs
2015-04-03 13:38:44 +00:00

63 lines
2.1 KiB
Haskell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# LANGUAGE OverloadedStrings #-}
module Ldap.Client.ModifySpec (spec) where
import Data.ByteString (ByteString)
import Data.Monoid ((<>))
import Test.Hspec
import Ldap.Client as Ldap
import SpecHelper (locally, charizard, pikachu)
spec :: Spec
spec = do
let go l f = Ldap.search l (Dn "o=localhost")
(Ldap.scope WholeSubtree <> Ldap.typesOnly True)
f
[]
context "delete" $ do
it "can land charizard" $ do
res <- locally $ \l -> do
[x] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") x `shouldBe` Just ["fire", "flying"]
Ldap.modify l charizard [Attr "type" `Delete` ["flying"]]
[y] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") y `shouldBe` Just ["fire"]
res `shouldBe` Right ()
it "tries to remove pikachu's password, unsuccessfully" $ do
res <- locally $ \l -> do
Ldap.modify l pikachu [Attr "password" `Delete` []]
res `shouldBe` Left
(ModifyError (ModifyErrorCode UnwillingToPerform (Dn "o=localhost") "cannot delete password"))
context "add" $ do
it "can feed charizard" $ do
res <- locally $ \l -> do
[x] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") x `shouldBe` Just ["fire", "flying"]
Ldap.modify l charizard [Attr "type" `Add` ["fed"]]
[y] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") y `shouldBe` Just ["fire", "flying", "fed"]
res `shouldBe` Right ()
context "replace" $ do
it "can put charizard to sleep" $ do
res <- locally $ \l -> do
[x] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") x `shouldBe` Just ["fire", "flying"]
Ldap.modify l charizard [Attr "type" `Replace` ["sleeping"]]
[y] <- go l (Attr "cn" := "charizard")
lookupAttr (Attr "type") y `shouldBe` Just ["sleeping"]
res `shouldBe` Right ()
lookupAttr :: Attr -> SearchEntry -> Maybe [ByteString]
lookupAttr a (SearchEntry _ as) = lookup a as