Skip to content
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

unit test and fix for null values in rendered JSON in UserProfile #1292

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/wire-api/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ tests:
- base
- bytestring-conversion
- wire-api
- uuid
- aeson-qq
- lens
- swagger2
Expand Down
30 changes: 15 additions & 15 deletions libs/wire-api/src/Wire/API/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,21 @@ modelUser = Doc.defineModel "User" $ do

instance ToJSON UserProfile where
toJSON u =
object
[ "id" .= qUnqualified (profileQualifiedId u),
"qualified_id" .= profileQualifiedId u,
"name" .= profileName u,
"picture" .= profilePict u,
"assets" .= profileAssets u,
"accent_id" .= profileAccentId u,
"deleted" .= (if profileDeleted u then Just True else Nothing),
"service" .= profileService u,
"handle" .= profileHandle u,
"locale" .= profileLocale u,
"expires_at" .= profileExpire u,
"team" .= profileTeam u,
"email" .= profileEmail u
]
object $
"id" .= qUnqualified (profileQualifiedId u)
# "qualified_id" .= profileQualifiedId u
# "name" .= profileName u
# "picture" .= profilePict u
# "assets" .= profileAssets u
# "accent_id" .= profileAccentId u
# "deleted" .= (if profileDeleted u then Just True else Nothing)
# "service" .= profileService u
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pair

# "handle" .= profileHandle u
# "locale" .= profileLocale u
# "expires_at" .= profileExpire u
# "team" .= profileTeam u
# "email" .= profileEmail u
# []

instance FromJSON UserProfile where
parseJSON = withObject "UserProfile" $ \o ->
Expand Down
26 changes: 23 additions & 3 deletions libs/wire-api/test/unit/Test/Wire/API/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,38 @@
module Test.Wire.API.User where

import Data.Aeson
import qualified Data.Aeson as Aeson
import Data.Aeson.Types as Aeson
import Data.Domain
import Data.Id
import Data.Qualified
import qualified Data.UUID.V4 as UUID
import Imports
import Test.Tasty
import Test.Tasty.HUnit
import Wire.API.User (parseIdentity)
import Wire.API.User.Identity
import Wire.API.User

tests :: TestTree
tests = testGroup "User (types vs. aeson)" $ unitTests

unitTests :: [TestTree]
unitTests =
unitTests = parseIdentityTests ++ jsonNullTests

jsonNullTests :: [TestTree]
jsonNullTests = [testGroup "JSON null" [testCase "userProfile" $ testUserProfile]]

testUserProfile :: Assertion
testUserProfile = do
uid <- Id <$> UUID.nextRandom
let domain = Domain "example.com"
let colour = ColourId 0
let userProfile = UserProfile (Qualified uid domain) (Name "name") (Pict []) [] colour False Nothing Nothing Nothing Nothing Nothing Nothing
let profileJSONAsText = show $ Aeson.encode userProfile
let msg = "toJSON encoding must not convert Nothing to null, but instead omit those json fields for backwards compatibility. UserProfileJSON:" <> profileJSONAsText
assertBool msg (not $ "null" `isInfixOf` profileJSONAsText)

parseIdentityTests :: [TestTree]
parseIdentityTests =
[ let (=#=) :: Either String (Maybe UserIdentity) -> (Maybe UserSSOId, [Pair]) -> Assertion
(=#=) uid (mssoid, object -> Object obj) = assertEqual "=#=" uid (parseEither (parseIdentity mssoid) obj)
(=#=) _ bad = error $ "=#=: impossible: " <> show bad
Expand Down
3 changes: 2 additions & 1 deletion libs/wire-api/wire-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 4e9f1f53fb43a39da1366fe060fe0ef8e2b0185847abdc61552ca3262bf25322
-- hash: 46c27a7dd27a8d410e856a84b3d86983e03db46567976decb2856d18e0d2068f

name: wire-api
version: 0.1.0
Expand Down Expand Up @@ -153,5 +153,6 @@ test-suite wire-api-tests
, tasty-quickcheck
, types-common >=0.16
, unordered-containers
, uuid
, wire-api
default-language: Haskell2010