Skip to content

Commit 5749430

Browse files
Merge pull request #4 from oxydev/issue1-addtests-and-workflow
fix #1 fix #2 fix #3
2 parents 3a453c3 + 18163c0 commit 5749430

File tree

5 files changed

+170
-32
lines changed

5 files changed

+170
-32
lines changed

.github/workflows/test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: Python package
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
python-version: [ 3.7, 3.8, 3.9 ]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up Python ${{ matrix.python-version }}
24+
uses: actions/setup-python@v2
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
python -m pip install requests
31+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
32+
- name: Test with unittest
33+
run: python -m unittest

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# SubsCrypt Python Package
2-
2+
[![Python Test](https://github.com/oxydev/SubsCrypt-python-package/actions/workflows/test.yml/badge.svg)](https://github.com/oxydev/SubsCrypt-python-package/actions/workflows/test.yml)
33
[![PyPI license](https://img.shields.io/pypi/l/subscrypt.svg)](https://pypi.python.org/pypi/subscrypt/)
44
[![PyPI version fury.io](https://badge.fury.io/py/subscrypt.svg)](https://pypi.python.org/pypi/subscrypt/)
55

66
The python interface for interacting with SubsCrypt Service.
77

8-
<img src="https://oxydev.github.io/SubsCrypt-docs/images/logo2.png" width="400">
8+
<img src="https://oxydev.github.io/SubsCrypt-docs/images/logo.jpg" width="400">
99

1010
# Installation
1111

config.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"nodeAddress": "http://206.189.154.160:3000/",
3+
"test": {
4+
"userAddress": "5HpG9w8EBLe5XCrbczpwq5TSXvedjrBGCwqxK1iQ7qUsSWFc",
5+
"providerAddress": "5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
6+
"passWord": "password",
7+
"username": "user",
8+
"providerUsername": "provider",
9+
"plansData": [
10+
{
11+
"duration": "86,400,000",
12+
"price": "1,000,000",
13+
"max_refund_permille_policy": "100",
14+
"disabled": false
15+
},
16+
{
17+
"duration": "864,000,000",
18+
"price": "9,000,000",
19+
"max_refund_permille_policy": "150",
20+
"disabled": false
21+
},
22+
{
23+
"duration": "1,728,000,000",
24+
"price": "16,000,000",
25+
"max_refund_permille_policy": "200",
26+
"disabled": false
27+
}
28+
],
29+
"plansCharacteristic": [
30+
[
31+
"plan0"
32+
],
33+
[
34+
"plan1_0",
35+
"plan1_1"
36+
],
37+
[
38+
"plan2_0",
39+
"plan2_1",
40+
"plan2_2"
41+
]
42+
]
43+
}
44+
}

subscrypt/client.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from urllib.parse import urljoin
2-
2+
import json
33
import requests
44

5+
with open('config.json', 'r') as f:
6+
config = json.load(f)
7+
58

69
class Subscrypt:
710
def __init__(
811
self,
9-
base_url: str = 'http://206.189.154.160:3000/'
12+
base_url: str = config['nodeAddress']
1013
):
1114
self.base_url = base_url
1215

@@ -23,100 +26,88 @@ def check_subscription(self, user_address: str, provider_address: str, plan_inde
2326
providerAddress=provider_address,
2427
planIndex=plan_index,
2528
)
26-
29+
2730
def check_subscription_with_username(self, username: str, provider_address: str, plan_index: int):
2831
return self._get(
2932
f'/subsCrypt/checkSubscription/{username}/',
3033
providerAddress=provider_address,
3134
planIndex=plan_index,
3235
)
33-
36+
3437
def get_username(self, sender: str):
3538
return self._get(
3639
f'/subsCrypt/getUsername/{sender}/',
3740
)
38-
41+
3942
def retrieve_whole_data_with_username(self, username: str, password: str):
4043
return self._get(
4144
'/subsCrypt/retrieveDataWithUsername/',
4245
username=username,
4346
phrase=password,
4447
)
45-
48+
4649
def retrieve_data_with_username(self, user_address: str, provider_address: str, password: str):
4750
return self._get(
4851
f'/subsCrypt/retrieveDataWithUsername/{provider_address}/',
4952
username=user_address,
5053
phrase=password,
5154
)
52-
53-
def retrieve_data_with_wallet(self, sender: str, provider_address: str):
54-
# TODO (there is no api doc in swagger)
55-
raise NotImplemented
56-
57-
def retrieve_whole_data_with_wallet(self, sender: str):
58-
# TODO (there is no api doc in swagger)
59-
raise NotImplemented
6055

6156
def is_username_available(self, username: str):
6257
return self._get(
6358
f'/subsCrypt/isUsernameAvailable/{username}',
6459
)
65-
60+
6661
def check_auth(self, user_address: str, provider_address: str, password: str):
6762
return self._get(
6863
'/subsCrypt/checkAuth/',
6964
userAddress=user_address,
7065
providerAddress=provider_address,
7166
phrase=password,
7267
)
73-
68+
7469
def check_auth_with_username(self, username: str, provider_address: str, password: str):
7570
return self._get(
7671
f'/subsCrypt/checkAuth/{username}/',
7772
providerAddress=provider_address,
7873
phrase=password,
7974
)
80-
75+
8176
def user_check_auth(self, user_address: str, password: str):
8277
return self._get(
8378
'/subsCrypt/userCheckAuth/',
8479
userAddress=user_address,
8580
phrase=password,
8681
)
87-
82+
8883
def user_check_auth_with_username(self, username: str, password: str):
8984
return self._get(
9085
f'/subsCrypt/userCheckAuth/{username}/',
9186
phrase=password,
9287
)
93-
88+
9489
def get_plan_data(self, provider_address: str, plan_index: int):
9590
return self._get(
9691
f'/subsCrypt/getPlanData/{provider_address}/{plan_index}/',
9792
)
98-
93+
9994
def provider_check_auth(self, provider_address: str, password: str):
10095
return self._get(
10196
'/subsCrypt/providerCheckAuth/',
10297
providerAddress=provider_address,
10398
phrase=password,
10499
)
105-
100+
106101
def provider_check_auth_with_username(self, provider_username: str, password: str):
107102
return self._get(
108103
f'/subsCrypt/providerCheckAuth/{provider_username}/',
109104
phrase=password,
110105
)
111-
106+
112107
def get_plan_characteristics(self, provider_address: str, plan_index: int):
113108
return self._get(
114109
f'/subsCrypt/getPlanCharacteristics/{provider_address}/{plan_index}/',
115110
)
116-
117-
def get_sha2(self, string: str):
118-
# TODO (there is no api doc in swagger)
119-
raise NotImplemented
120111

121112
def is_connected(self):
122113
return self._get(

tests.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,81 @@
1-
from unittest import TestCase
2-
1+
import unittest
32
from subscrypt import Subscrypt
3+
import json
4+
5+
with open('config.json', 'r') as f:
6+
config = json.load(f)
47

58

6-
class TestSubscrypt(TestCase):
9+
class TestSubscrypt(unittest.TestCase):
710
def setUp(self):
811
self.client = Subscrypt()
912

1013
def test_is_connected_returns_true(self):
11-
self.assertTrue(self.client.is_connected())
14+
self.assertEqual(self.client.is_connected(), True)
15+
16+
def test_check_subscription_returns_bool(self):
17+
self.assertIn(
18+
self.client.check_subscription(config['test']['userAddress'], config['test']['providerAddress'], 0),
19+
[True, False])
20+
21+
def test_check_subscription_with_username_returns_bool(self):
22+
self.assertIn(
23+
self.client.check_subscription(config['test']['username'], config['test']['providerAddress'], 0),
24+
[True, False])
25+
26+
def test_get_username_works(self):
27+
self.assertEqual(self.client.get_username(config['test']['userAddress']), config['test']['username'])
28+
29+
def test_retrieve_whole_data_with_username_works(self):
30+
self.assertIsNotNone(self.client.retrieve_whole_data_with_username(
31+
config['test']['username'], config['test']['passWord']))
32+
33+
def test_retrieve_data_with_username_works(self):
34+
self.assertIsNotNone(self.client.retrieve_data_with_username(
35+
config['test']['username'], config['test']['providerAddress'], config['test']['passWord']))
36+
37+
def test_is_username_available_returns_false(self):
38+
self.assertEqual(self.client.is_username_available(config['test']['username']), False)
39+
40+
def test_check_auth_true(self):
41+
self.assertEqual(self.client.check_auth(config['test']['userAddress'], config['test']['providerAddress'],
42+
config['test']['passWord']), True)
43+
44+
def test_check_auth_with_username_true(self):
45+
self.assertEqual(
46+
self.client.check_auth_with_username(config['test']['username'], config['test']['providerAddress'],
47+
config['test']['passWord']), True)
48+
49+
def test_user_check_auth_with_username_true(self):
50+
self.assertEqual(
51+
self.client.user_check_auth_with_username(config['test']['username'],
52+
config['test']['passWord']), True)
53+
54+
def test_get_plan_data(self):
55+
plans = config['test']['plansData']
56+
index = 0
57+
for plan in plans:
58+
self.assertEqual(
59+
self.client.get_plan_data(config['test']['providerAddress'],
60+
index), plan)
61+
index += 1
62+
63+
def test_provider_check_auth_true(self):
64+
self.assertEqual(
65+
self.client.provider_check_auth(config['test']['providerAddress'],
66+
config['test']['passWord']), True)
67+
68+
def test_provider_check_auth_with_username_true(self):
69+
self.assertEqual(
70+
self.client.provider_check_auth_with_username(config['test']['providerUsername'],
71+
config['test']['passWord']), True)
72+
73+
def test_get_plan_data(self):
74+
for i in range(len(config['test']['plansData'])):
75+
self.assertEqual(
76+
self.client.get_plan_characteristics(config['test']['providerAddress'],
77+
i), config['test']['plansCharacteristic'][i])
78+
79+
80+
if __name__ == '__main__':
81+
unittest.main()

0 commit comments

Comments
 (0)