diff options
Diffstat (limited to 'test/polyjuice/client_test.exs')
-rw-r--r-- | test/polyjuice/client_test.exs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs index a0e53ec..e546f72 100644 --- a/test/polyjuice/client_test.exs +++ b/test/polyjuice/client_test.exs @@ -188,6 +188,8 @@ defmodule Polyjuice.ClientTest do test "login" do {:ok, tmpdir} = TestUtil.mktmpdir("sync-") + storage = Polyjuice.Client.Storage.Ets.open() + try do tmpdir_charlist = to_charlist(tmpdir) @@ -212,6 +214,7 @@ defmodule Polyjuice.ClientTest do "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/", access_token: nil, sync: false, + storage: storage, test: true ) @@ -228,8 +231,22 @@ defmodule Polyjuice.ClientTest do end ) == {"m.id.user_login", "@alice:example.org", "foo"} + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") == + "m.id.user_login" + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") == + "@alice:example.org" + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id") == "foo" + Polyjuice.Client.log_out(client) + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") == + nil + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") == nil + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id") == nil + assert Agent.get( Polyjuice.Client.process_name(client.id, :state), fn %{ @@ -296,7 +313,69 @@ defmodule Polyjuice.ClientTest do :inets.stop(:httpd, httpd_pid) after + Polyjuice.Client.Storage.close(storage) File.rm_rf(tmpdir) end end + + test "stores and retrieves from storage on creation" do + storage = Polyjuice.Client.Storage.Ets.open() + + # if we start a client and specify the access token, user ID, and device + # ID, then those values get stored + client = + Polyjuice.Client.start_link( + "http://127.0.0.1:8008/", + access_token: "an_access_token", + user_id: "@alice:example.org", + device_id: "a_device", + sync: false, + storage: storage, + test: true + ) + + assert Agent.get( + Polyjuice.Client.process_name(client.id, :state), + fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end + ) == {"an_access_token", "@alice:example.org", "a_device"} + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") == + "an_access_token" + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") == + "@alice:example.org" + + assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id") == + "a_device" + + Polyjuice.Client.stop(client) + + # if we start a client and don't specify them, but they're stored, use the + # stored values + + client2 = + Polyjuice.Client.start_link( + "http://127.0.0.1:8008/", + sync: false, + storage: storage, + test: true + ) + + assert Agent.get( + Polyjuice.Client.process_name(client2.id, :state), + fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end + ) == {"an_access_token", "@alice:example.org", "a_device"} + end end |