|
| 1 | +""" API Tests """ |
| 2 | +import unittest |
| 3 | +import woocommerce |
| 4 | +from httmock import all_requests, HTTMock |
| 5 | + |
| 6 | + |
| 7 | +class WooCommerceTestCase(unittest.TestCase): |
| 8 | + """Test case for the client methods.""" |
| 9 | + |
| 10 | + def setUp(self): |
| 11 | + self.consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 12 | + self.consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 13 | + self.api = woocommerce.API( |
| 14 | + url="http://woo.test", |
| 15 | + consumer_key=self.consumer_key, |
| 16 | + consumer_secret=self.consumer_secret |
| 17 | + ) |
| 18 | + |
| 19 | + def test_version(self): |
| 20 | + """ Test default version """ |
| 21 | + api = woocommerce.API( |
| 22 | + url="https://woo.test", |
| 23 | + consumer_key=self.consumer_key, |
| 24 | + consumer_secret=self.consumer_secret |
| 25 | + ) |
| 26 | + |
| 27 | + self.assertEqual(api.version, "v3") |
| 28 | + |
| 29 | + def test_non_ssl(self): |
| 30 | + """ Test non-ssl """ |
| 31 | + api = woocommerce.API( |
| 32 | + url="http://woo.test", |
| 33 | + consumer_key=self.consumer_key, |
| 34 | + consumer_secret=self.consumer_secret |
| 35 | + ) |
| 36 | + self.assertFalse(api.is_ssl) |
| 37 | + |
| 38 | + def test_with_ssl(self): |
| 39 | + """ Test non-ssl """ |
| 40 | + api = woocommerce.API( |
| 41 | + url="https://woo.test", |
| 42 | + consumer_key=self.consumer_key, |
| 43 | + consumer_secret=self.consumer_secret |
| 44 | + ) |
| 45 | + self.assertTrue(api.is_ssl, True) |
| 46 | + |
| 47 | + def test_get(self): |
| 48 | + """ Test GET requests """ |
| 49 | + @all_requests |
| 50 | + def woo_test_mock(*args, **kwargs): |
| 51 | + """ URL Mock """ |
| 52 | + return {'status_code': 200, |
| 53 | + 'content': 'OK'} |
| 54 | + |
| 55 | + with HTTMock(woo_test_mock): |
| 56 | + # call requests |
| 57 | + status = self.api.get("products").status_code |
| 58 | + self.assertEqual(status, 200) |
| 59 | + |
| 60 | + def test_post(self): |
| 61 | + """ Test POST requests """ |
| 62 | + @all_requests |
| 63 | + def woo_test_mock(*args, **kwargs): |
| 64 | + """ URL Mock """ |
| 65 | + return {'status_code': 201, |
| 66 | + 'content': 'OK'} |
| 67 | + |
| 68 | + with HTTMock(woo_test_mock): |
| 69 | + # call requests |
| 70 | + status = self.api.post("products", {}).status_code |
| 71 | + self.assertEqual(status, 201) |
| 72 | + |
| 73 | + def test_put(self): |
| 74 | + """ Test PUT requests """ |
| 75 | + @all_requests |
| 76 | + def woo_test_mock(*args, **kwargs): |
| 77 | + """ URL Mock """ |
| 78 | + return {'status_code': 200, |
| 79 | + 'content': 'OK'} |
| 80 | + |
| 81 | + with HTTMock(woo_test_mock): |
| 82 | + # call requests |
| 83 | + status = self.api.put("products", {}).status_code |
| 84 | + self.assertEqual(status, 200) |
| 85 | + |
| 86 | + def test_delete(self): |
| 87 | + """ Test DELETE requests """ |
| 88 | + @all_requests |
| 89 | + def woo_test_mock(*args, **kwargs): |
| 90 | + """ URL Mock """ |
| 91 | + return {'status_code': 200, |
| 92 | + 'content': 'OK'} |
| 93 | + |
| 94 | + with HTTMock(woo_test_mock): |
| 95 | + # call requests |
| 96 | + status = self.api.delete("products").status_code |
| 97 | + self.assertEqual(status, 200) |
0 commit comments