From 0ff06cf2c831cc9283d2e539978fedf599df2b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Sat, 23 Nov 2024 21:19:46 +0100 Subject: [PATCH] [chore] refactor test function test_base64_encoding_decoding --- tests/src/AuthorizationTest.idr | 43 +++++++++++++++------------------ 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/tests/src/AuthorizationTest.idr b/tests/src/AuthorizationTest.idr index b82dc2a..0dc88e7 100644 --- a/tests/src/AuthorizationTest.idr +++ b/tests/src/AuthorizationTest.idr @@ -11,26 +11,23 @@ import Utils.String export test_base64_encoding_decoding : EitherT String IO () -test_base64_encoding_decoding = - let user = "aladdin" - passwd = "opensesame" - (_, basicauth) = applyBasicAuth user passwd - basicauth' = base64DecodeString $ last $ split ((==) ' ') basicauth - in case basicauth' of - Nothing => - idris_crash "couldn't base64 decode string." - Just basicauth'' => - case utf8_pack basicauth'' of - Nothing => - idris_crash "couldn't utf8 pack decoded string." - Just basicauth''' => - let userpasswd = split ((==) ':') basicauth''' - user' = head userpasswd - passwd' = last userpasswd - in case basicauth == "Basic YWxhZGRpbjpvcGVuc2VzYW1l" && - user == user' && - passwd == passwd' of - True => - pure () - False => - throwE "base64 encode/decode error: expected \{user} and \{user'}, \{passwd} and \{passwd'} to be equivalent." +test_base64_encoding_decoding = do + let user = "aladdin" + let passwd = "opensesame" + let (_, basicauth) = applyBasicAuth user passwd + + basicauth' <- maybe (throwE "Couldn't base64 decode string.") pure $ + base64DecodeString $ last $ split ((==) ' ') basicauth + + basicauth'' <- maybe (throwE "Couldn't utf8 pack decoded string.") pure $ + utf8_pack basicauth' + + let userpasswd = split ((==) ':') basicauth'' + let user' = head userpasswd + let passwd' = last userpasswd + + if basicauth == "Basic YWxhZGRpbjpvcGVuc2VzYW1l" && + user == user' && + passwd == passwd' + then pure () + else throwE $ "Base64 encode/decode error: expected {user} and {user'}, {passwd} and {passwd'} to be equivalent."