SNAPKITTYWEST commited on
Commit
9a31c8d
·
verified ·
1 Parent(s): 9c54f86

Add test/hk_browser_capability_tests.erl

Browse files
test/hk_browser_capability_tests.erl ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -module(hk_browser_capability_tests).
2
+ -include_lib("eunit/include/eunit.hrl").
3
+
4
+ bare_atom_grant_permits_operation_test() ->
5
+ ?assert(hk_browser_capability:is_granted([read_page, click], read_page)),
6
+ ?assertNot(hk_browser_capability:is_granted([read_page], click)).
7
+
8
+ navigate_domain_scoped_grant_test() ->
9
+ Caps = [{navigate, #{allowed_domains => [<<"example.com">>]}}],
10
+ ?assert(hk_browser_capability:is_granted(Caps, {navigate, #{url => <<"https://example.com/x">>}})),
11
+ ?assertNot(hk_browser_capability:is_granted(Caps, {navigate, #{url => <<"https://evil.example/">>}})).
12
+
13
+ unrestricted_navigate_grant_permits_any_domain_test() ->
14
+ ?assert(hk_browser_capability:is_granted([navigate], {navigate, #{url => <<"https://anything.example/">>}})).
15
+
16
+ validate_args_rejects_non_http_scheme_test() ->
17
+ ?assertMatch({error, _}, hk_browser_capability:validate_args(navigate, #{url => <<"javascript:alert(1)">>})),
18
+ ?assertMatch({ok, _}, hk_browser_capability:validate_args(navigate, #{url => <<"https://example.com">>})).
19
+
20
+ validate_args_rejects_oversized_selector_test() ->
21
+ Huge = binary:copy(<<"a">>, 3000),
22
+ ?assertMatch({error, _}, hk_browser_capability:validate_args(click, #{selector => Huge})).
23
+
24
+ validate_args_requires_text_for_type_test() ->
25
+ ?assertMatch({error, missing_text}, hk_browser_capability:validate_args(type, #{selector => <<"#x">>})),
26
+ ?assertMatch({ok, _}, hk_browser_capability:validate_args(type, #{selector => <<"#x">>, text => <<"hi">>})).