-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#119] Change set-field Web-API #128
Changes from 3 commits
a56bbda
f9c5fa6
4a373e2
e5e088d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,22 @@ module SetField.SetFieldTest where | |
|
||
import Control.Exception (try) | ||
import Control.Lens | ||
import Data.Aeson | ||
import Data.Aeson (Value) | ||
import Data.Aeson.Lens | ||
import Data.Aeson.QQ.Simple (aesonQQ) | ||
import Data.Functor (void) | ||
import Data.Text (Text) | ||
import Data.Time | ||
import Network.HTTP.Req | ||
import Network.HTTP.Types (status400, status404) | ||
import Network.HTTP.Types (status404) | ||
import Test.Tasty.HUnit | ||
import Utils | ||
|
||
unit_set_new_field :: IO () | ||
unit_set_new_field = cofferTest do | ||
createEntry "entry" | ||
|
||
response <- setField "entry" "newField" "contents" | ||
response <- setField "entry" "newField" Nothing "contents" | ||
response @= | ||
[aesonQQ| | ||
{ | ||
|
@@ -41,26 +42,16 @@ unit_set_new_field = cofferTest do | |
unit_set_field_private :: IO () | ||
unit_set_field_private = cofferTest do | ||
createEntry "entry" | ||
setField "entry" "public-field" "contents" | ||
response <- | ||
executeCommand | ||
POST | ||
["set-field", "private"] | ||
NoReqBody | ||
(jsonResponse @Value) | ||
( mconcat | ||
[ "path" =: ("entry" :: Text) | ||
, "field" =: ("public-field" :: Text) | ||
] | ||
) | ||
|
||
response <- setField "entry" "private-field" (Just False) "contents" | ||
|
||
let fieldMb = | ||
responseBody response | ||
& scrubDates | ||
& findField "public-field" | ||
& findField "private-field" | ||
|
||
case fieldMb of | ||
Nothing -> assertFailure $ "Expected field \"public-field\", found nothing" | ||
Nothing -> assertFailure $ "Expected field \"private-field\", found nothing" | ||
Just value -> | ||
value @?= | ||
[aesonQQ| | ||
|
@@ -73,18 +64,9 @@ unit_set_field_private = cofferTest do | |
|
||
unit_set_field_public :: IO () | ||
unit_set_field_public = cofferTest do | ||
unit_set_field_private | ||
response <- | ||
executeCommand | ||
POST | ||
["set-field", "public"] | ||
NoReqBody | ||
(jsonResponse @Value) | ||
( mconcat | ||
[ "path" =: ("entry" :: Text) | ||
, "field" =: ("public-field" :: Text) | ||
] | ||
) | ||
createEntry "entry" | ||
|
||
response <- setField "entry" "public-field" (Just True) "contents" | ||
|
||
let fieldMb = | ||
responseBody response | ||
|
@@ -105,7 +87,7 @@ unit_set_field_public = cofferTest do | |
|
||
unit_set_field_entry_not_found :: IO () | ||
unit_set_field_entry_not_found = cofferTest do | ||
try @HttpException (setField "entry" "field" "contents") >>= | ||
try @HttpException (setField "entry" "field" Nothing "contents") >>= | ||
processStatusCodeError status404 | ||
[aesonQQ| | ||
[ | ||
|
@@ -116,40 +98,16 @@ unit_set_field_entry_not_found = cofferTest do | |
] | ||
|] | ||
|
||
unit_set_field_missing_field_contents :: IO () | ||
unit_set_field_missing_field_contents = cofferTest do | ||
createEntry "entry" | ||
|
||
try @HttpException | ||
( | ||
executeCommand | ||
POST | ||
["set-field"] | ||
(ReqBodyJson Null) | ||
(jsonResponse @Value) | ||
( mconcat | ||
[ "path" =: ("entry" :: Text) | ||
, "field" =: ("field" :: Text) | ||
] | ||
) | ||
) >>= | ||
processStatusCodeError status400 | ||
[aesonQQ| | ||
[ | ||
{ | ||
"error": "The entry at '/entry' does not yet have a field 'field'.\nIn order to create a new field, please include 'FIELDCONTENTS' in the body.", | ||
"code": 301 | ||
} | ||
] | ||
|] | ||
-- unit_set_field_missing_field_contents :: IO () | ||
-- TODO: add test that expects an error in case of missing contents | ||
|
||
unit_set_field_updates_modification_date :: IO () | ||
unit_set_field_updates_modification_date = cofferTest do | ||
createEntry "entry" | ||
setField "entry" "field" "contents" | ||
setField "entry" "field" Nothing "contents" | ||
|
||
t1 <- getCurrentTime | ||
response <- setField "entry" "field" "contents2" | ||
response <- setField "entry" "field" Nothing "contents2" | ||
t2 <- getCurrentTime | ||
|
||
let newModifiedDate = responseBody response ^?! key "dateModified" . _JSON @_ @UTCTime | ||
|
@@ -158,3 +116,51 @@ unit_set_field_updates_modification_date = cofferTest do | |
|
||
assertBool "Dates are not equal" | ||
$ responseBody response ^?! key "fields" . key "field" . key "dateModified" . _JSON == newModifiedDate | ||
|
||
unit_set_field_visibility :: IO () | ||
unit_set_field_visibility = cofferTest do | ||
createEntry "dir/entry" | ||
void $ setField "dir/entry" "private-field" Nothing "contents" | ||
void $ setField "dir/entry" "public-field" Nothing "contents" | ||
changeFieldVisibility "dir/entry" "private-field" "private" | ||
changeFieldVisibility "dir/entry" "public-field" "public" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why there is two checks in one test? If we're expecting to fail at once = one way (only private/public) I know that they're just doing the same thing, but we aren't short on test space) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed 👍 |
||
|
||
response <- | ||
executeCommand | ||
GET | ||
["view"] | ||
NoReqBody | ||
(jsonResponse @Value) | ||
("path" =: (("dir/entry") :: Text)) | ||
|
||
response @= | ||
[aesonQQ| | ||
{ | ||
"entries": [], | ||
"subdirs": { | ||
"dir": { | ||
"entries": [ | ||
{ | ||
"path": "/dir/entry", | ||
"dateModified": "", | ||
"masterField": null, | ||
"fields": { | ||
"public-field": { | ||
"dateModified": "", | ||
"visibility": "public", | ||
"contents": "contents" | ||
}, | ||
"private-field": { | ||
"dateModified": "", | ||
"visibility": "private", | ||
"contents": "contents" | ||
} | ||
}, | ||
"tags": [] | ||
} | ||
], | ||
"subdirs": {} | ||
} | ||
} | ||
} | ||
|] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems as unsolved TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved now, you can check how I did it, I'm not sure it is the proper way