simbld_http/responses/
local.rs

1use crate::generate_responses_functions;
2use crate::responses::CustomResponse;
3use crate::traits::get_code_trait::GetCode;
4use strum_macros::EnumIter;
5
6generate_responses_functions! {
7"Local API responses",
8  ResponsesLocalApiCodes,
9  ApprovedNoActionRequired => (200, "OK", "Operation approved, no action needed.", 900, "Approved No Action Required"),
10  Approved => (200, "OK", "Operation successfully approved.", 901, "Approved"),
11  DuplicatedTransactionId => (400, "Bad Request", "Duplicate transaction ID; already processed.", 902, "Duplicated Transaction ID"),
12  ValidationErrorsProvided => (400, "Bad Request", "Validation errors occurred; check provided values.", 903, "Validation Errors Provided"),
13  OperationNotAllowed => (403, "Forbidden", "The requested operation is not permitted.", 904, "Operation Not Allowed"),
14  OperationNotSupported => (400, "Bad Request", "The requested operation is not supported.", 905, "Operation Not Supported"),
15  TransactionTimeout => (408, "Request Timeout", "Transaction could not complete before it expired.", 906, "Transaction Timeout"),
16  AuthentificationFailed => (401, "Unauthorized", "Authentication failed (incorrect credentials).", 907, "Authentification Failed"),
17  DoNotHonor => (402, "Payment Required", "General decline, insufficient funds or no reason provided.", 908, "Do Not Honor"),
18  InsufficientFunds => (402, "Payment Required", "Account has insufficient funds.", 909, "Insufficient Funds"),
19  IncorrectPIN => (400, "Bad Request", "PIN code is incorrect.", 910, "Incorrect PIN"),
20  InvalidTransaction => (400, "Bad Request", "Transaction request is invalid/unsupported.", 911, "Invalid Transaction"),
21  InvalidAmount => (400, "Bad Request", "Specified amount is invalid.", 912, "Invalid Amount"),
22  InvalidCardNumber => (400, "Bad Request", "Card number (PAN) is invalid or not accepted.", 913, "Invalid Card Number"),
23  InvalidCVV => (400, "Bad Request", "CVV code is invalid.", 914, "Invalid CVV"),
24  InvalidCardHolderName => (400, "Bad Request", "Card holder name is invalid.", 915, "Invalid Card Holder Name"),
25  InvalidCardHolderLastName => (400, "Bad Request", "Card holder's last name is invalid.", 916, "Invalid Card Holder Last Name"),
26  InvalidCardHolderFirstName => (400, "Bad Request", "Card holder's first name is invalid.", 917, "Invalid Card Holder First Name"),
27  InvalidCardHolderIdNumber => (400, "Bad Request", "Card holder's ID number is invalid.", 918, "Invalid Card Holder ID Number"),
28  InvalidCardHolderPhoneNumber => (400, "Bad Request", "Card holder's phone number is invalid.", 919, "Invalid Card Holder Phone Number"),
29  CardAlreadyActive => (400, "Bad Request", "Card is already active, cannot re-activate.", 920, "Card Already Active"),
30  CardNotActive => (400, "Bad Request", "Card is not active or not found.", 921, "Card Not Active"),
31  ExpiredCard => (400, "Bad Request", "Card has expired.", 922, "Expired Card"),
32  LostCard => (400, "Bad Request", "Card was reported lost.", 923, "Lost Card"),
33  InvalidLastName => (400, "Bad Request", "Last name provided is invalid.", 925, "Invalid Last Name"),
34  InvalidFirstName => (400, "Bad Request", "First name provided is invalid.", 926, "Invalid First Name"),
35  InvalidIdNumber => (400, "Bad Request", "ID number provided is invalid.", 927, "Invalid ID Number"),
36  InvalidPhoneNumber => (400, "Bad Request", "Phone number provided is invalid.", 928, "Invalid Phone Number"),
37  InvalidEmail => (400, "Bad Request", "Email address provided is invalid.", 929, "Invalid Email"),
38  InvalidInitials => (400, "Bad Request", "Initials provided are invalid.", 930, "Invalid Initials"),
39  InvalidAddress => (400, "Bad Request", "Address provided is invalid.", 931, "Invalid Address"),
40  InvalidCity => (400, "Bad Request", "City provided is invalid.", 932, "Invalid City"),
41  InvalidPostalCode => (400, "Bad Request", "Postal code provided is invalid.", 933, "Invalid Postal Code"),
42  InvalidCountry => (400, "Bad Request", "Country provided is invalid.", 934, "Invalid Country"),
43  InvalidPassword => (400, "Bad Request", "Password provided is invalid.", 935, "Invalid Password"),
44  InvalidUsername => (400, "Bad Request", "Username provided is invalid.", 936, "Invalid Username"),
45  InvalidRole => (400, "Bad Request", "Role specified is invalid.", 937, "Invalid Role"),
46  InvalidStatus => (400, "Bad Request", "Status specified is invalid.", 938, "Invalid Status"),
47  InvalidDateOfBirth => (400, "Bad Request", "Date of birth provided is invalid.", 939, "Invalid Date of Birth"),
48  InvalidMajority => (400, "Bad Request", "Majority information is invalid.", 940, "Invalid Majority"),
49  InvalidMaritalStatus => (400, "Bad Request", "Marital status is invalid.", 941, "Invalid Marital Status"),
50  InvalidNationality => (400, "Bad Request", "Nationality provided is invalid.", 942, "Invalid Nationality"),
51  InvalidLanguage => (400, "Bad Request", "Language provided is invalid.", 943, "Invalid Language"),
52  InvalidCurrency => (400, "Bad Request", "Currency provided is invalid.", 944, "Invalid Currency"),
53  InvalidTimeZone => (400, "Bad Request", "Time zone specified is invalid.", 945, "Invalid Time Zone"),
54  InvalidProfilePicture => (400, "Bad Request", "Profile picture is invalid or unsupported.", 946, "Invalid Profile Picture"),
55  InvalidCoverPicture => (400, "Bad Request", "Cover picture is invalid or unsupported.", 947, "Invalid Cover Picture"),
56  InvalidBio => (400, "Bad Request", "Bio provided is invalid.", 948, "Invalid Bio"),
57  InvalidWebsite => (400, "Bad Request", "Website URL provided is invalid.", 949, "Invalid Website"),
58  InvalidFacebook => (400, "Bad Request", "Facebook profile name provided is invalid.", 950, "Invalid Facebook"),
59  InvalidTwitter => (400, "Bad Request", "Twitter profile name provided is invalid.", 951, "Invalid Twitter"),
60  InvalidInstagram => (400, "Bad Request", "Instagram profile name provided is invalid.", 952, "Invalid Instagram"),
61  InvalidLinkedin => (400, "Bad Request", "LinkedIn profile name provided is invalid.", 953, "Invalid LinkedIn"),
62  InvalidGithub => (400, "Bad Request", "GitHub profile name provided is invalid.", 954, "Invalid GitHub"),
63  InvalidGitlab => (400, "Bad Request", "GitLab profile name provided is invalid.", 955, "Invalid GitLab"),
64  InvalidBitbucket => (400, "Bad Request", "Bitbucket profile name provided is invalid.", 956, "Invalid Bitbucket"),
65  InvalidGoogle => (400, "Bad Request", "Google profile name provided is invalid.", 957, "Invalid Google"),
66  InvalidYoutube => (400, "Bad Request", "YouTube profile name provided is invalid.", 958, "Invalid YouTube"),
67  InvalidTwitch => (400, "Bad Request", "Twitch profile name provided is invalid.", 959, "Invalid Twitch"),
68  InvalidDiscord => (400, "Bad Request", "Discord profile name provided is invalid.", 960, "Invalid Discord"),
69  InvalidSlack => (400, "Bad Request", "Slack profile name provided is invalid.", 961, "Invalid Slack"),
70  InvalidTelegram => (400, "Bad Request", "Telegram profile name provided is invalid.", 962, "Invalid Telegram"),
71  InvalidWhatsapp => (400, "Bad Request", "WhatsApp info provided is invalid.", 963, "Invalid WhatsApp"),
72  InvalidSkype => (400, "Bad Request", "Skype name/ID provided is invalid.", 964, "Invalid Skype"),
73  InvalidSnapchat => (400, "Bad Request", "Snapchat name provided is invalid.", 965, "Invalid Snapchat"),
74  InvalidPinterest => (400, "Bad Request", "Pinterest name provided is invalid.", 966, "Invalid Pinterest"),
75  InvalidTumblr => (400, "Bad Request", "Tumblr name provided is invalid.", 967, "Invalid Tumblr"),
76  InvalidFlickr => (400, "Bad Request", "Flickr name provided is invalid.", 968, "Invalid Flickr"),
77  InvalidVimeo => (400, "Bad Request", "Vimeo name provided is invalid.", 969, "Invalid Vimeo"),
78  InvalidSoundCloud => (400, "Bad Request", "SoundCloud name provided is invalid.", 970, "Invalid SoundCloud"),
79  InvalidSpotify => (400, "Bad Request", "Spotify name provided is invalid.", 971, "Invalid Spotify"),
80  InvalidAppleMusic => (400, "Bad Request", "Apple Music name provided is invalid.", 972, "Invalid Apple Music"),
81  InvalidTidal => (400, "Bad Request", "Tidal name provided is invalid.", 973, "Invalid Tidal"),
82  InvalidDeezer => (400, "Bad Request", "Deezer name provided is invalid.", 974, "Invalid Deezer"),
83  InvalidAmazonMusic => (400, "Bad Request", "Amazon Music name provided is invalid.", 975, "Invalid Amazon Music"),
84  InvalidBandcamp => (400, "Bad Request", "Bandcamp name provided is invalid.", 976, "Invalid Bandcamp"),
85  InvalidMixcloud => (400, "Bad Request", "Mixcloud name provided is invalid.", 977, "Invalid Mixcloud"),
86  InvalidSnap => (400, "Bad Request", "Snap profile name provided is invalid.", 978, "Invalid Snap"),
87  InvalidReddit => (400, "Bad Request", "Reddit profile name provided is invalid.", 979, "Invalid Reddit"),
88  InvalidTikTok => (400, "Bad Request", "TikTok profile name provided is invalid.", 980, "Invalid TikTok"),
89
90}
91
92#[cfg(test)]
93mod tests {
94    use crate::helpers::unified_tuple_helper::UnifiedTuple;
95    use crate::responses::ResponsesLocalApiCodes;
96    use crate::traits::tuple_traits::IntoTwoFieldsTuple;
97    use serde_json::json;
98    use serde_json::to_value;
99
100    #[test]
101    fn test_local_api_codes_get_code() {
102        assert_eq!(ResponsesLocalApiCodes::Approved.get_code(), 200);
103        assert_eq!(ResponsesLocalApiCodes::InvalidCardNumber.get_code(), 400);
104        assert_eq!(ResponsesLocalApiCodes::InvalidCVV.get_code(), 400);
105        assert_eq!(ResponsesLocalApiCodes::InvalidEmail.get_code(), 400);
106    }
107
108    #[test]
109    fn test_local_api_codes_from_u16() {
110        assert_eq!(ResponsesLocalApiCodes::from_u16(914), Some(ResponsesLocalApiCodes::InvalidCVV));
111        assert_eq!(
112            ResponsesLocalApiCodes::from_u16(929),
113            Some(ResponsesLocalApiCodes::InvalidEmail)
114        );
115        assert_eq!(
116            ResponsesLocalApiCodes::from_u16(957),
117            Some(ResponsesLocalApiCodes::InvalidGoogle)
118        );
119        assert_eq!(ResponsesLocalApiCodes::from_u16(9999), None);
120    }
121
122    #[test]
123    fn test_local_api_codes_as_tuple() {
124        let code = ResponsesLocalApiCodes::InvalidPassword;
125        let tuple = UnifiedTuple {
126            standard_code: 400,
127            standard_name: "Bad Request",
128            unified_description: "Password provided is invalid.",
129            internal_code: Some(935),
130            internal_name: Option::from("Invalid Password"),
131        };
132        let code_as_tuple = code.as_tuple();
133        assert_eq!(code_as_tuple, tuple);
134    }
135
136    #[test]
137    fn test_local_api_codes_as_json() {
138        let response_code = ResponsesLocalApiCodes::InvalidPhoneNumber;
139        let json_result = response_code.as_json();
140        let expected_json = json!({
141            "type": "Local API responses",
142            "details": {
143                "standard http code": {
144                    "code": 400,
145                    "name": "Bad Request"
146                },
147                "description": "Phone number provided is invalid.",
148                "internal http code": {
149                    "code": 928,
150                    "name": "Invalid Phone Number"
151                }
152            }
153        });
154
155        assert_eq!(json_result, expected_json);
156    }
157
158    #[test]
159    fn test_operation_not_supported_codes_into_two_fields_tuple() {
160        let responses_code = ResponsesLocalApiCodes::OperationNotSupported;
161        let tuple = responses_code.into_two_fields_tuple();
162        let json_result = to_value(&tuple).unwrap();
163
164        let expected_json = json!({
165            "code": 400,
166            "name": "Bad Request"
167        });
168
169        assert_eq!(json_result, expected_json);
170    }
171
172    #[test]
173    fn test_bad_request_duplicate_standard_codes() {
174        // These two codes have the same standard HTTP code (400) but different internal codes
175        assert_eq!(
176            ResponsesLocalApiCodes::from_u16(949),
177            Some(ResponsesLocalApiCodes::InvalidWebsite)
178        );
179        assert_eq!(
180            ResponsesLocalApiCodes::from_u16(910),
181            Some(ResponsesLocalApiCodes::IncorrectPIN)
182        );
183    }
184}