This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
230 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
defmodule BuilderTest do | ||
alias ExUcan.Core.Capability | ||
alias ExUcan.Core.Structs.UcanPayload | ||
alias ExUcan.Builder | ||
use ExUnit.Case | ||
|
||
setup do | ||
keypair = ExUcan.create_default_keypair() | ||
%{keypair: keypair} | ||
end | ||
|
||
@tag :build | ||
test "builder functions, default" do | ||
assert %Builder{issuer: nil} = Builder.default() | ||
end | ||
|
||
@tag :build | ||
test "non-working builder flow", _meta do | ||
assert {:error, "must call issued_by/2"} = Builder.default() |> Builder.build() | ||
end | ||
|
||
@tag :build | ||
test "non-working builder flow, need for_audience", meta do | ||
assert {:error, "must call for_audience/2"} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.build() | ||
end | ||
|
||
@tag :build | ||
test "non-working builder flow, need with_lifetime", meta do | ||
assert {:error, "must call with_lifetime/2 or with_expiration/2"} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.build() | ||
end | ||
|
||
@tag :build | ||
test "working builder flow", meta do | ||
assert {:ok, %UcanPayload{}} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_lifetime(86_400) | ||
|> Builder.build() | ||
end | ||
|
||
@tag :build | ||
test "working builder flow, with expiration", meta do | ||
assert {:ok, %UcanPayload{}} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.build() | ||
end | ||
|
||
@tag :build | ||
test "more workflows", meta do | ||
assert {:ok, %UcanPayload{fct: %{"door" => "bronze"}, nnc: nnc}} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.with_fact("door", "bronze") | ||
|> Builder.with_nonce() | ||
|> Builder.not_before((DateTime.utc_now() |> DateTime.to_unix()) - 86_400) | ||
|> Builder.build() | ||
|
||
assert is_binary(nnc) | ||
end | ||
|
||
@tag :build | ||
test "with capabilities", meta do | ||
cap = Capability.new("example://bar", "ability/bar", %{"beep" => 1}) | ||
|
||
assert {:ok, %UcanPayload{cap: caps}} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.claiming_capability(cap) | ||
|> Builder.build() | ||
|
||
assert [%{resource: "example://bar", ability: "ability/bar"} | _] = caps | ||
end | ||
|
||
@tag :build | ||
test "with bang variant success", meta do | ||
cap = Capability.new("example://bar", "ability/bar", %{"beep" => 1}) | ||
|
||
assert %UcanPayload{cap: caps} = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.claiming_capability(cap) | ||
|> Builder.build!() | ||
|
||
assert [%{resource: "example://bar", ability: "ability/bar"} | _] = caps | ||
end | ||
|
||
@tag :build | ||
test "with bang variant fail", meta do | ||
cap = Capability.new("example://bar", "ability/bar", %{"beep" => 1}) | ||
|
||
res = | ||
try do | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.claiming_capability(cap) | ||
|> Builder.build!() | ||
rescue | ||
e in RuntimeError -> e | ||
end | ||
|
||
assert %RuntimeError{message: _} = res | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,60 @@ | ||
defmodule ExUcanTest do | ||
alias ExUcan.Builder | ||
alias ExUcan.Keymaterial.Ed25519.Keypair | ||
use ExUnit.Case | ||
doctest ExUcan | ||
|
||
setup do | ||
keypair = ExUcan.create_default_keypair() | ||
%{keypair: keypair} | ||
end | ||
|
||
test "create_default_keypair" do | ||
assert %Keypair{jwt_alg: "EdDSA"} = keypair = Keypair.create() | ||
assert is_binary(keypair.public_key) | ||
assert is_binary(keypair.secret_key) | ||
end | ||
|
||
@tag :exucan | ||
test "validate_token, success", meta do | ||
token = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.build!() | ||
|> ExUcan.sign(meta.keypair) | ||
|> ExUcan.encode() | ||
|
||
assert :ok = ExUcan.validate_token(token) | ||
end | ||
|
||
@tag :exucan | ||
test "invalid token, due to expiry", meta do | ||
token = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) - 5) | ||
|> Builder.build!() | ||
|> ExUcan.sign(meta.keypair) | ||
|> ExUcan.encode() | ||
|
||
assert {:error, "Ucan token is already expired"} = ExUcan.validate_token(token) | ||
end | ||
|
||
@tag :exucan | ||
test "invalid token, too early", meta do | ||
token = | ||
Builder.default() | ||
|> Builder.issued_by(meta.keypair) | ||
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD") | ||
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400) | ||
|> Builder.not_before((DateTime.utc_now() |> DateTime.to_unix()) + (div(86400, 2))) | ||
|> Builder.build!() | ||
|> ExUcan.sign(meta.keypair) | ||
|> ExUcan.encode() | ||
|
||
assert {:error, "Ucan token is not yet active"} = ExUcan.validate_token(token) | ||
end | ||
end |