Commit 895f3895 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Call setupEnvironment in each DB-related test

parent 1230ea3a
......@@ -22,9 +22,10 @@ import Control.Monad
import Control.Monad.Reader
import Gargantext.Database.Action.User.New
import Gargantext.Core.Types
import Test.API.Setup (withTestDBAndPort)
import Test.API.Setup (withTestDBAndPort, setupEnvironment)
import qualified Data.Text as T
import Control.Lens
import Data.Maybe
auth_api :: AuthRequest -> ClientM AuthResponse
auth_api = client (Proxy :: Proxy (MkGargAPI (GargAPIVersion AuthAPI)))
......@@ -34,6 +35,8 @@ cannedToken = "eyJhbGciOiJIUzUxMiJ9.eyJkYXQiOnsiaWQiOjF9fQ.t49zZSqkPAulEkYEh4pW1
tests :: Spec
tests = sequential $ aroundAll withTestDBAndPort $ do
describe "Prelude" $ do
it "setup DB triggers" $ \((testEnv, _), _) -> setupEnvironment testEnv
describe "Authentication" $ do
baseUrl <- runIO $ parseBaseUrl "http://localhost"
manager <- runIO $ newManager defaultManagerSettings
......@@ -56,17 +59,17 @@ tests = sequential $ aroundAll withTestDBAndPort $ do
let authPayload = AuthRequest "alice" (GargPassword "alice")
result0 <- runClientM (auth_api authPayload) (clientEnv port)
let result = over (_Right . authRes_valid . _Just . authVal_token) (const cannedToken) result0
let expected = AuthResponse {
_authRes_valid = Just $
AuthValid {
_authVal_token = cannedToken
, _authVal_tree_id = NodeId 1
, _authVal_user_id = 1
, _authVal_tree_id = fromMaybe (NodeId 1) $ listToMaybe $ result0 ^.. _Right . authRes_valid . _Just . authVal_tree_id
, _authVal_user_id = fromMaybe 1 $ listToMaybe $ result0 ^.. _Right . authRes_valid . _Just . authVal_user_id
}
, _authRes_inval = Nothing
}
let result = over (_Right . authRes_valid . _Just . authVal_token) (const cannedToken) result0
result `shouldBe` (Right expected)
it "denies login for user 'alice' if password is invalid" $ \((_testEnv, port), _) -> do
......
......@@ -21,7 +21,7 @@ import Servant
import Servant.Auth.Client ()
import Servant.Client
import Test.API.Authentication (auth_api)
import Test.API.Setup (withTestDBAndPort)
import Test.API.Setup (withTestDBAndPort, setupEnvironment)
import Test.Database.Types
import Test.Hspec
import Test.Hspec.Wai hiding (pendingWith)
......@@ -70,6 +70,8 @@ withValidLogin port ur pwd act = do
tests :: Spec
tests = sequential $ aroundAll withTestDBAndPort $ do
describe "Prelude" $ do
it "setup DB triggers" $ \((testEnv, _), _) -> setupEnvironment testEnv
describe "Private API" $ do
baseUrl <- runIO $ parseBaseUrl "http://localhost"
manager <- runIO $ newManager defaultManagerSettings
......
......@@ -10,7 +10,11 @@ import Gargantext.API.Admin.Types
import Gargantext.API.Prelude
import Gargantext.Core.NLP
import Gargantext.Core.NodeStory
import Gargantext.Database.Action.Flow
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
import Gargantext.Database.Admin.Trigger.Init
import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node (getOrMkList)
import Gargantext.Prelude.Config
import Gargantext.System.Logging
import Network.HTTP.Client.TLS (newTlsManager)
......@@ -28,6 +32,10 @@ import qualified Gargantext.Utils.Jobs.Queue as Jobs
import qualified Gargantext.Utils.Jobs.Settings as Jobs
import qualified Network.Wai.Handler.Warp as Warp
import qualified Servant.Job.Async as ServantAsync
import Gargantext.Database.Admin.Types.Hyperdata
import Control.Monad.Reader
import Gargantext.Core.Types.Individu
import Gargantext.Database.Action.User.New
newTestEnv :: TestEnv -> Logger (GargM Env GargError) -> Warp.Port -> IO Env
......@@ -80,3 +88,14 @@ withTestDBAndPort action =
makeApp env
withGargApp app $ \port ->
action ((testEnv, port), app)
setupEnvironment :: TestEnv -> IO ()
setupEnvironment env = flip runReaderT env $ runTestMonad $ do
void $ initFirstTriggers "secret_key"
void $ new_user $ mkNewUser (userMaster <> "@cnrs.com") (GargPassword "secret_key")
(masterUserId, _masterRootId, masterCorpusId)
<- getOrMk_RootWithCorpus (UserName userMaster)
(Left corpusMasterName)
(Nothing :: Maybe HyperdataCorpus)
masterListId <- getOrMkList masterCorpusId masterUserId
void $ initLastTriggers masterListId
......@@ -19,16 +19,14 @@ import Gargantext.Database.Action.User
import Gargantext.Database.Action.User.New
import Gargantext.Database.Admin.Types.Hyperdata.Corpus
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Query.Table.Node (mk, getCorporaWithParentId, getOrMkList)
import Gargantext.Database.Query.Table.Node
import Gargantext.Database.Query.Tree.Root (getRootId)
import Gargantext.Database.Schema.Node (NodePoly(..))
import Gargantext.Prelude
import Prelude
import Test.API.Setup (setupEnvironment)
import qualified Data.Text as T
import Gargantext.Database.Action.Flow
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
import Gargantext.Database.Admin.Trigger.Init
import Test.Database.Operations.DocumentSearch
import Test.Database.Setup (withTestDB)
import Test.Database.Types
......@@ -77,17 +75,6 @@ instance Eq a => Eq (ExpectedActual a) where
(Actual a) == (Expected b) = a == b
_ == _ = False
setupEnvironment :: TestEnv -> IO ()
setupEnvironment env = flip runReaderT env $ runTestMonad $ do
void $ initFirstTriggers "secret_key"
void $ new_user $ mkNewUser (userMaster <> "@cnrs.com") (GargPassword "secret_key")
(masterUserId, _masterRootId, masterCorpusId)
<- getOrMk_RootWithCorpus (UserName userMaster)
(Left corpusMasterName)
(Nothing :: Maybe HyperdataCorpus)
masterListId <- getOrMkList masterCorpusId masterUserId
void $ initLastTriggers masterListId
writeRead01 :: TestEnv -> Assertion
writeRead01 env = do
flip runReaderT env $ runTestMonad $ do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment