1+ #!/usr/bin/env python3
2+ import os
3+ import unittest
4+ from src .lighthouseweb3 import Lighthouse
5+ from .setup import parse_env
6+ import requests as req
7+ from src .lighthouseweb3 .functions .config import Config
8+ from web3 import Web3
9+ from eth_account .messages import encode_defunct
10+
11+
12+ class TestGetApiKey (unittest .TestCase ):
13+
14+ def test_get_api_key (self ):
15+ """test getApiKey using valid signed message and public key"""
16+ parse_env ()
17+ publicKey = os .environ .get ("PUBLIC_KEY" )
18+ response = req .get (
19+ f"{ Config .lighthouse_api } /api/auth/get_auth_message?publicKey={ publicKey } "
20+ )
21+
22+ if (response .status_code != 200 ):
23+ raise Exception ("Failed to get authentication message" )
24+
25+ verificationMessage = response .json ()
26+
27+ self .assertIn ("Please prove you are the owner" , verificationMessage , "Owner response should come" )
28+
29+ encodedMessage = encode_defunct (text = verificationMessage )
30+
31+
32+ signedMessage = Web3 ().eth .account .sign_message (
33+ encodedMessage ,
34+ private_key = os .environ .get ("PRIVATE_KEY" )
35+ ).signature .hex ()
36+
37+ res = Lighthouse .getApiKey (publicKey , f"0x{ signedMessage } " )
38+
39+ self .assertIsInstance (res , dict , "res is a dict" )
40+ self .assertIsInstance (res .get ("data" ), dict , "data is a dict" )
41+ self .assertIsInstance (res .get ('data' ).get ('apiKey' ), str , "apiKey is a string" )
42+
43+ def test_get_api_key_with_invalid_message (self ):
44+ """test getApiKey using signed invalid message and public key"""
45+ parse_env ()
46+ publicKey = os .environ .get ("PUBLIC_KEY" )
47+ encodedMessage = encode_defunct (text = 'random_message' )
48+
49+
50+ signedMessage = Web3 ().eth .account .sign_message (
51+ encodedMessage ,
52+ private_key = os .environ .get ("PRIVATE_KEY" )
53+ ).signature .hex ()
54+
55+ res = Lighthouse .getApiKey (publicKey , f"0x{ signedMessage } " )
56+ self .assertIsInstance (res , dict , "res is a dict" )
57+ self .assertIsInstance (res .get ("error" ), dict , "data is a dict" )
58+
59+ def test_get_api_key_with_random_private_key (self ):
60+ """test getApiKey using signed message with invalid private key and public key"""
61+
62+ parse_env ()
63+ publicKey = os .environ .get ("PUBLIC_KEY" )
64+ response = req .get (
65+ f"{ Config .lighthouse_api } /api/auth/get_auth_message?publicKey={ publicKey } "
66+ )
67+
68+ if (response .status_code != 200 ):
69+ raise Exception ("Failed to get authentication message" )
70+
71+ verificationMessage = response .json ()
72+
73+ self .assertIn ("Please prove you are the owner" , verificationMessage , "Owner response should come" )
74+
75+ encodedMessage = encode_defunct (text = verificationMessage )
76+
77+
78+ signedMessage = Web3 ().eth .account .sign_message (
79+ encodedMessage ,
80+ private_key = '0x8218aa5dbf4dbec243142286b93e26af521b3e91219583595a06a7765abc9c8b'
81+ ).signature .hex ()
82+
83+ res = Lighthouse .getApiKey (publicKey , f"0x{ signedMessage } " )
84+
85+ self .assertIsInstance (res , dict , "res is a dict" )
86+ self .assertIsInstance (res .get ("error" ), dict , "data is a dict" )
0 commit comments