Skip to content

Commit bfc231b

Browse files
feat: add tests
1 parent 3fafca3 commit bfc231b

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

tests/test_decrypt.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
from src.phase import Phase
3+
import src
4+
5+
APP_ID = "phApp:v1:e0e50cb9a1953c610126b4092093b1beca51d08d91fc3d9f8d90482a32853215"
6+
APP_SECRET = "pss:v1:d261abecb6708c18bebdb8b2748ee574e2b0bdeaf19b081a5f10006cc83d48d0:d146c8c6d326a7842ff9b2da0da455b3f7f568a70808e2eb0cfc5143d4fe170f:59e413612e06d75d251e3416361d0743345a9c9eda1cbcf2b1ef16e3077c011c"
7+
APP_SECRET_INVALID = "pss:v1:d251abecb6708c18bebdb8b2748ee574e2b0bdeaf19b081a5f10006cc83d48d0:d146c8c6d326a7842ff9b2da0da455b3f7f568a70808e2eb0cfc5143d4fe170d:59e413612e06d75d251e3416361d0743345a9c9eda1cbcf2b1ef16e3077c012d"
8+
9+
10+
@pytest.fixture(scope="module")
11+
def phase_instance():
12+
return Phase(APP_ID, APP_SECRET)
13+
14+
15+
def mock_fetch_app_key(appToken, wrapKey, appId, dataSize):
16+
return "e35ae9560207c90fa3dd68a8715e13a1ef988bffa284db73f04328df17f37cfe"
17+
18+
19+
def test_phase_decrypt_returns_correct_plaintext(phase_instance, monkeypatch):
20+
data = "Signal"
21+
22+
monkeypatch.setattr(src.phase, "fetch_app_key", mock_fetch_app_key)
23+
24+
ciphertext = phase_instance.encrypt(data)
25+
26+
plaintext = phase_instance.decrypt(ciphertext)
27+
28+
assert plaintext is not None
29+
assert plaintext == data
30+
31+
32+
def test_phase_decrypt_rejects_promise_with_incorrect_app_secret(monkeypatch):
33+
phase = Phase(APP_ID, APP_SECRET_INVALID)
34+
35+
monkeypatch.setattr(src.phase, "fetch_app_key", mock_fetch_app_key)
36+
37+
data = "Signal"
38+
ciphertext = phase.encrypt(data)
39+
40+
with pytest.raises(ValueError, match="Something went wrong"):
41+
phase.decrypt(ciphertext)

tests/test_encrypt.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
import re
3+
from src.phase import Phase
4+
5+
APP_ID = "phApp:v1:cd2d579490fd794f1640590220de86a3676fa7979d419056bc631741b320b701"
6+
APP_SECRET = "pss:v1:a7a0822aa4a4e4d37919009264200ba6ab978d92c8b4f7db5ae9ce0dfaf604fe:801605dfb89822ff52957abe39949bcfc44b9058ad81de58dd54fb0b110037b4b2bbde5a1143d31bbb3895f72e4ee52f5bd:625d395987f52c37022063eaf9b6260cad9ca03c99609213f899cae7f1bb04e7"
7+
8+
9+
@pytest.fixture(scope="module")
10+
def phase_instance():
11+
return Phase(APP_ID, APP_SECRET)
12+
13+
14+
def test_phase_encrypt_returns_valid_ph(phase_instance):
15+
plaintext = "Signal"
16+
tag = "Phase Tag"
17+
PH_VERSION = "v1"
18+
19+
ciphertext = phase_instance.encrypt(plaintext, tag)
20+
21+
assert ciphertext is not None
22+
segments = ciphertext.split(":")
23+
assert len(segments) == 5
24+
assert segments[0] == "ph"
25+
assert segments[1] == PH_VERSION
26+
assert segments[4] == tag
27+
assert re.match("^[0-9a-f]+$", segments[2]) is not None
28+
assert re.match(
29+
"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=$)", segments[3]) is not None
30+
31+
32+
def test_phase_encrypt_produces_same_length_ciphertexts(phase_instance):
33+
data = "hello world"
34+
num_of_trials = 10
35+
ciphertext_lengths = set()
36+
37+
for _ in range(num_of_trials):
38+
ciphertext = phase_instance.encrypt(data)
39+
ciphertext_lengths.add(len(ciphertext))
40+
41+
assert len(ciphertext_lengths) == 1

tests/test_init.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from src.phase import Phase
2+
import pytest
3+
4+
APP_ID_INVALID = "phApp:v1:cd2d579490fd794f1640590220de86a3676fa7979d419056bc631741b320b701"
5+
APP_SECRET_INVALID = "pss:v1:a7a0822aa4a4e4d37919009264200ba6ab978d92c8b4f7db5ae9ce0dfaf604fe:801605dfb89822ff52957abe39949bcfc44b9058ad81de58dd54fb0b110037b4b2bbde5a1143d31bbb3895f72e4ee52f5bd:625d395987f52c37022063eaf9b6260cad9ca03c99609213f899cae7f1bb04e7"
6+
7+
8+
@pytest.fixture(scope="module")
9+
def phase_instance():
10+
return Phase(APP_ID_INVALID, APP_SECRET_INVALID)
11+
12+
13+
def test_invalid_app_id(phase_instance):
14+
invalid_app_id = "phApp:version:cd2d579490fd794f1640590220de86a3676fa7979d419056bc631741b320b701"
15+
with pytest.raises(ValueError, match="Invalid Phase APP_ID"):
16+
Phase(invalid_app_id, APP_SECRET_INVALID)
17+
18+
19+
def test_invalid_app_secret(phase_instance):
20+
invalid_app_secret = "pss:v1:00000000000000000000000000000000:00000000000000000000000000000000:00000000000000000000000000000000"
21+
with pytest.raises(ValueError, match="Invalid Phase APP_SECRET"):
22+
Phase(APP_ID_INVALID, invalid_app_secret)

0 commit comments

Comments
 (0)