diff --git a/bunq/sdk/context.py b/bunq/sdk/context.py index f2778dd..f685ed2 100644 --- a/bunq/sdk/context.py +++ b/bunq/sdk/context.py @@ -264,8 +264,17 @@ def save(self, path=None): if path is None: path = self._PATH_API_CONTEXT_DEFAULT - with open(path, self._FILE_MODE_WRITE) as file: - file.write(converter.class_to_json(self)) + with open(path, self._FILE_MODE_WRITE) as file_: + file_.write(self.to_json()) + + def to_json(self): + """ + Serializes an ApiContext to JSON string. + + :rtype: str + """ + + return converter.class_to_json(self) @classmethod def restore(cls, path=None): @@ -278,8 +287,25 @@ def restore(cls, path=None): if path is None: path = cls._PATH_API_CONTEXT_DEFAULT - with open(path, cls._FILE_MODE_READ) as file: - return converter.json_to_class(ApiContext, file.read()) + with open(path, cls._FILE_MODE_READ) as file_: + return cls.from_json(file_.read()) + + @classmethod + def from_json(cls, json_str): + """ + Creates an ApiContext instance from JSON string. + + :type json_str: str + + :rtype: ApiContext + """ + + return converter.json_to_class(ApiContext, json_str) + + def __eq__(self, other): + return (self.token == other.token and + self.api_key == other.api_key and + self.environment_type == other.environment_type) class InstallationContext(object): diff --git a/tests/README.md b/tests/README.md index 8215f8f..b2ea44f 100644 --- a/tests/README.md +++ b/tests/README.md @@ -37,6 +37,16 @@ of a configuration file is located at [`tests/assets/config.example.json`](./ass In order to make use of the configuration file, please copy the example to the same directory, fill in your sandbox user data and rename the copy to config.json. +Note: +* `MONETARY_ACCOUNT_ID` and `MONETARY_ACCOUNT_ID2` must be of same user +* `CounterPartyOther` must be of another Sandbox user +* You can create a `CASH_REGISTER_ID` on doc.bunq.com + 1. Add your **Developer Key** to `settings` + 2. Upload an image to the `Attachment Public` endpoint + 3. Create an `Avatar` with the returned UUID + 4. Use the Avatar's UUID to create a `Cash Register` + 5. Copy the Cash Register's ID to the `config.json` + ## Execution You can run the tests via command line: diff --git a/tests/model/generated/test_api_context.py b/tests/model/generated/test_api_context.py new file mode 100644 index 0000000..ed44d13 --- /dev/null +++ b/tests/model/generated/test_api_context.py @@ -0,0 +1,85 @@ +import os + +from bunq.sdk.context import ApiContext +from bunq.sdk.json import converter +from tests.bunq_test import BunqSdkTestCase + + +class ApiContextTest(BunqSdkTestCase): + """ + Tests: + ApiContext + """ + + _TMP_FILE_PATH = '/context-save-test.conf' + + @classmethod + def setUpClass(cls): + cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ + cls._API_CONTEXT = cls._get_api_context() + cls._TMP_FILE_PATH_FULL = (cls._get_directory_test_root() + + cls._TMP_FILE_PATH) + + def test_api_context_save(self): + """ + Converts an ApiContext to JSON data, saves the same ApiContext to a + temporary file, and compares whether the JSON data is equal to the + data in the file. + + Removes the temporary file before assertion. + """ + + context_json = converter.class_to_json(self._API_CONTEXT) + + self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) + + with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file_: + context_retrieved = file_.read() + + os.remove(self._TMP_FILE_PATH_FULL) + + self.assertEqual(context_retrieved, context_json) + + def test_api_context_restore(self): + """ + Saves an ApiContext to a temporary file, restores an ApiContext from + that file, and compares whether the api_keys, tokens, and environment + types are equal in the ApiContext and the restored ApiContext. + + Removes the temporary file before assertion. + """ + + self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) + api_context_restored = ApiContext.restore(self._TMP_FILE_PATH_FULL) + + os.remove(self._TMP_FILE_PATH_FULL) + + self.assertEqual(api_context_restored, self._API_CONTEXT) + + def test_api_context_save_json(self): + """ + Converts an ApiContext to JSON data, saves the ApiContext using the + ApiContext.save() function with the to_JSON flag set to True, and + compares whether the JSON data equals the returned JSON data from the + ApiContext.save() function. + """ + + context_json = converter.class_to_json(self._API_CONTEXT) + context_saved = self._API_CONTEXT.to_json() + + self.assertEqual(context_saved, context_json) + + def test_api_context_restore_json(self): + """ + Saves an ApiContext with the ApiContext.save() function with the + to_JSON flag set to True, restores an ApiContext from the JSON data + returned from the ApiContext.save() function, and checks that the + api_key, token, and environment type variables of the restored + ApiContext are equal to the respective variables in the original + ApiContext. + """ + + context_json = self._API_CONTEXT.to_json() + api_context_restored = self._API_CONTEXT.from_json(context_json) + + self.assertEqual(api_context_restored, self._API_CONTEXT)