|
| 1 | +module Feature.RollbackSpec where |
| 2 | + |
| 3 | +import Network.Wai (Application) |
| 4 | + |
| 5 | +import Network.HTTP.Types |
| 6 | +import Test.Hspec |
| 7 | +import Test.Hspec.Wai |
| 8 | +import Test.Hspec.Wai.JSON |
| 9 | + |
| 10 | +import Protolude hiding (get) |
| 11 | +import SpecHelper |
| 12 | + |
| 13 | +-- two helpers functions to make sure that each test can setup and cleanup properly |
| 14 | + |
| 15 | +-- creates Item to work with for PATCH and DELETE |
| 16 | +postItem = |
| 17 | + request methodPost "/items" |
| 18 | + [("Prefer", "resolution=ignore-duplicates")] |
| 19 | + [json|{"id":0}|] |
| 20 | + `shouldRespondWith` |
| 21 | + "" |
| 22 | + { matchStatus = 201 } |
| 23 | + |
| 24 | +-- removes Items left over from POST, PUT, and PATCH |
| 25 | +deleteItems = |
| 26 | + delete "/items?id=lte.0" |
| 27 | + `shouldRespondWith` |
| 28 | + "" |
| 29 | + { matchStatus = 204 } |
| 30 | + |
| 31 | +preferDefault = [("Prefer", "return=representation")] |
| 32 | +preferCommit = [("Prefer", "return=representation"), ("Prefer", "tx=commit")] |
| 33 | +preferRollback = [("Prefer", "return=representation"), ("Prefer", "tx=rollback")] |
| 34 | + |
| 35 | +withoutPreferenceApplied = [] |
| 36 | +withPreferenceCommitApplied = [ "Preference-Applied" <:> "tx=commit" ] |
| 37 | +withPreferenceRollbackApplied = [ "Preference-Applied" <:> "tx=rollback" ] |
| 38 | + |
| 39 | +shouldRespondToReads reqHeaders respHeaders = do |
| 40 | + it "responds to GET" $ do |
| 41 | + request methodGet "/items?id=eq.1" |
| 42 | + reqHeaders |
| 43 | + "" |
| 44 | + `shouldRespondWith` |
| 45 | + [json|[{"id":1}]|] |
| 46 | + { matchHeaders = respHeaders } |
| 47 | + |
| 48 | + it "responds to HEAD" $ do |
| 49 | + request methodHead "/items?id=eq.1" |
| 50 | + reqHeaders |
| 51 | + "" |
| 52 | + `shouldRespondWith` |
| 53 | + "" |
| 54 | + { matchHeaders = respHeaders } |
| 55 | + |
| 56 | + it "responds to GET on RPC" $ do |
| 57 | + request methodGet "/rpc/search?id=1" |
| 58 | + reqHeaders |
| 59 | + "" |
| 60 | + `shouldRespondWith` |
| 61 | + [json|[{"id":1}]|] |
| 62 | + { matchHeaders = respHeaders } |
| 63 | + |
| 64 | + it "responds to POST on RPC" $ do |
| 65 | + request methodPost "/rpc/search" |
| 66 | + reqHeaders |
| 67 | + [json|{"id":1}|] |
| 68 | + `shouldRespondWith` |
| 69 | + [json|[{"id":1}]|] |
| 70 | + { matchHeaders = respHeaders } |
| 71 | + |
| 72 | +shouldPersistMutations reqHeaders respHeaders = do |
| 73 | + it "does persist post" $ do |
| 74 | + request methodPost "/items" |
| 75 | + reqHeaders |
| 76 | + [json|{"id":0}|] |
| 77 | + `shouldRespondWith` |
| 78 | + [json|[{"id":0}]|] |
| 79 | + { matchStatus = 201 |
| 80 | + , matchHeaders = respHeaders } |
| 81 | + get "items?id=eq.0" |
| 82 | + `shouldRespondWith` |
| 83 | + [json|[{"id":0}]|] |
| 84 | + deleteItems |
| 85 | + |
| 86 | + it "does persist put" $ do |
| 87 | + request methodPut "/items?id=eq.0" |
| 88 | + reqHeaders |
| 89 | + [json|{"id":0}|] |
| 90 | + `shouldRespondWith` |
| 91 | + [json|[{"id":0}]|] |
| 92 | + { matchHeaders = respHeaders } |
| 93 | + get "items?id=eq.0" |
| 94 | + `shouldRespondWith` |
| 95 | + [json|[{"id":0}]|] |
| 96 | + deleteItems |
| 97 | + |
| 98 | + it "does persist patch" $ do |
| 99 | + postItem |
| 100 | + request methodPatch "/items?id=eq.0" |
| 101 | + reqHeaders |
| 102 | + [json|{"id":-1}|] |
| 103 | + `shouldRespondWith` |
| 104 | + [json|[{"id":-1}]|] |
| 105 | + { matchHeaders = respHeaders } |
| 106 | + get "items?id=eq.0" |
| 107 | + `shouldRespondWith` |
| 108 | + [json|[]|] |
| 109 | + get "items?id=eq.-1" |
| 110 | + `shouldRespondWith` |
| 111 | + [json|[{"id":-1}]|] |
| 112 | + deleteItems |
| 113 | + |
| 114 | + it "does persist delete" $ do |
| 115 | + postItem |
| 116 | + request methodDelete "/items?id=eq.0" |
| 117 | + reqHeaders |
| 118 | + "" |
| 119 | + `shouldRespondWith` |
| 120 | + [json|[{"id":0}]|] |
| 121 | + { matchHeaders = respHeaders } |
| 122 | + get "items?id=eq.0" |
| 123 | + `shouldRespondWith` |
| 124 | + [json|[]|] |
| 125 | + |
| 126 | +shouldNotPersistMutations reqHeaders respHeaders = do |
| 127 | + it "does not persist post" $ do |
| 128 | + request methodPost "/items" |
| 129 | + reqHeaders |
| 130 | + [json|{"id":0}|] |
| 131 | + `shouldRespondWith` |
| 132 | + [json|[{"id":0}]|] |
| 133 | + { matchStatus = 201 |
| 134 | + , matchHeaders = respHeaders } |
| 135 | + get "items?id=eq.0" |
| 136 | + `shouldRespondWith` |
| 137 | + [json|[]|] |
| 138 | + |
| 139 | + it "does not persist put" $ do |
| 140 | + request methodPut "/items?id=eq.0" |
| 141 | + reqHeaders |
| 142 | + [json|{"id":0}|] |
| 143 | + `shouldRespondWith` |
| 144 | + [json|[{"id":0}]|] |
| 145 | + { matchHeaders = respHeaders } |
| 146 | + get "items?id=eq.0" |
| 147 | + `shouldRespondWith` |
| 148 | + [json|[]|] |
| 149 | + |
| 150 | + it "does not persist patch" $ do |
| 151 | + request methodPatch "/items?id=eq.1" |
| 152 | + reqHeaders |
| 153 | + [json|{"id":0}|] |
| 154 | + `shouldRespondWith` |
| 155 | + [json|[{"id":0}]|] |
| 156 | + { matchHeaders = respHeaders } |
| 157 | + get "items?id=eq.0" |
| 158 | + `shouldRespondWith` |
| 159 | + [json|[]|] |
| 160 | + get "items?id=eq.1" |
| 161 | + `shouldRespondWith` |
| 162 | + [json|[{"id":1}]|] |
| 163 | + |
| 164 | + it "does not persist delete" $ do |
| 165 | + request methodDelete "/items?id=eq.1" |
| 166 | + reqHeaders |
| 167 | + "" |
| 168 | + `shouldRespondWith` |
| 169 | + [json|[{"id":1}]|] |
| 170 | + { matchHeaders = respHeaders } |
| 171 | + get "items?id=eq.1" |
| 172 | + `shouldRespondWith` |
| 173 | + [json|[{"id":1}]|] |
| 174 | + |
| 175 | +allowed :: SpecWith ((), Application) |
| 176 | +allowed = describe "tx-allow-override = true" $ do |
| 177 | + describe "without Prefer tx" $ do |
| 178 | + -- TODO: Change this to default to rollback for whole test-suite |
| 179 | + preferDefault `shouldRespondToReads` withoutPreferenceApplied |
| 180 | + preferDefault `shouldPersistMutations` withoutPreferenceApplied |
| 181 | + |
| 182 | + describe "Prefer tx=commit" $ do |
| 183 | + preferCommit `shouldRespondToReads` withPreferenceCommitApplied |
| 184 | + preferCommit `shouldPersistMutations` withPreferenceCommitApplied |
| 185 | + |
| 186 | + describe "Prefer tx=rollback" $ do |
| 187 | + preferRollback `shouldRespondToReads` withPreferenceRollbackApplied |
| 188 | + preferRollback `shouldNotPersistMutations` withPreferenceRollbackApplied |
| 189 | + |
| 190 | +disallowed :: SpecWith ((), Application) |
| 191 | +disallowed = describe "tx-rollback-all = false, tx-allow-override = false" $ do |
| 192 | + describe "without Prefer tx" $ do |
| 193 | + preferDefault `shouldRespondToReads` withoutPreferenceApplied |
| 194 | + preferDefault `shouldPersistMutations` withoutPreferenceApplied |
| 195 | + |
| 196 | + describe "Prefer tx=commit" $ do |
| 197 | + preferCommit `shouldRespondToReads` withoutPreferenceApplied |
| 198 | + preferCommit `shouldPersistMutations` withoutPreferenceApplied |
| 199 | + |
| 200 | + describe "Prefer tx=rollback" $ do |
| 201 | + preferRollback `shouldRespondToReads` withoutPreferenceApplied |
| 202 | + preferRollback `shouldPersistMutations` withoutPreferenceApplied |
| 203 | + |
| 204 | + |
| 205 | +forced :: SpecWith ((), Application) |
| 206 | +forced = describe "tx-rollback-all = true, tx-allow-override = false" $ do |
| 207 | + describe "without Prefer tx" $ do |
| 208 | + preferDefault `shouldRespondToReads` withoutPreferenceApplied |
| 209 | + preferDefault `shouldNotPersistMutations` withoutPreferenceApplied |
| 210 | + |
| 211 | + describe "Prefer tx=commit" $ do |
| 212 | + preferCommit `shouldRespondToReads` withoutPreferenceApplied |
| 213 | + preferCommit `shouldNotPersistMutations` withoutPreferenceApplied |
| 214 | + |
| 215 | + describe "Prefer tx=rollback" $ do |
| 216 | + preferRollback `shouldRespondToReads` withoutPreferenceApplied |
| 217 | + preferRollback `shouldNotPersistMutations` withoutPreferenceApplied |
| 218 | + |
0 commit comments