Skip to content

Commit

Permalink
Convert test/0 funs into eunit tests
Browse files Browse the repository at this point in the history
mochi reloader triggering the test/0 funs was bugging me. I'm not that
familiar with eunit so there will be bugs.
  • Loading branch information
andrewtj committed Oct 30, 2010
1 parent 93c4b90 commit 25598b2
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 120 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Mnesia.*
sasl_err*
erl_crash*
ebin/openid.app
deps/*
deps/*
.eunit/*
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{cover_enabled, true}.
{erl_opts, [fail_on_warning, debug_info]}.
{deps, [{ibrowse, ".*", {git, "git://github.com/andrewtj/ibrowse.git", "master"}}]}.
64 changes: 1 addition & 63 deletions src/openid.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%%%-------------------------------------------------------------------
-module(openid).

-export([discover/1, associate/1, authentication_url/3, test/0]).
-export([discover/1, associate/1, authentication_url/3]).

-include("openid.hrl").

Expand Down Expand Up @@ -215,65 +215,3 @@ authentication_url(AuthReq, ReturnTo, Realm) ->
[URL|_] = AuthReq#openid_authreq.opURLs,

list_to_binary([URL, "?", QueryString]).

%% ------------------------------------------------------------
%% Tests
%% ------------------------------------------------------------

test() ->

application:start(inets),
application:start(ssl),

Test = fun(ID) ->
?DBG({identifier, ID}),
Req = discover(ID),
%?DBG({request, Req}),
[URL,_] = Req#openid_authreq.opURLs,
Assoc = associate(URL),
?DBG({assoc, Assoc}),
%?DBG({auth, authenticate(Req2, "http://dev.brendonh.org/return", "http://dev.brendonh.org/")}),
ok
end,

% 2.0
Test("https://www.google.com/accounts/o8/id"),
%Test("http://flickr.com/exbrend"),
%Test("=brendonh"),

% 1.0 / 1.1
%?DBG({"AOL:", discover("http://openid.aol.com/brend")}),
%?DBG({"Myspace:", discover("www.myspace.com")}),
%?DBG({"LiveJournal:", discover("http://exbrend.livejournal.com")}),
%?DBG({"PaulBonser:", discover("blog.paulbonser.com")}),

application:stop(inets). % Avoid error spam from held-open connections




%% $ make test
%% erlc -o ebin -Wall -v +debug_info src/openid.erl
%% src/openid.erl:155: Warning: function unroll/1 is unused
%% erl +W w -pa ebin -noshell -pa ../mochiweb/ebin -s openid test -s init stop
%% <0.1.0>: {identifier,"https://www.google.com/accounts/o8/id"}
%% <0.1.0>: {request,{authReq,"https://www.google.com/accounts/o8/ud",
%% {2,0},
%% none,none}}
%% <0.1.0>: {assoc,{"46800",
%% "AO6d/PJErS+mEYxZNEsr3L/Tz6SvipjoQQW4TN8XxzHXTW8n4POIjk9kUBfl1yQvLF2rEmL4R3OqAKgDVsTIb9WzFF75+QmJXtXq5DqyQ4HRgBqgZk2RmijOHSKsVsZbsA==",
%% "CbImJ5wv7y1jgGCS3RWq5cCvByE="}}
%% <0.1.0>: {identifier,"http://flickr.com/exbrend"}
%% <0.1.0>: {request,{authReq,"https://open.login.yahooapis.com/openid/op/auth",
%% {2,0},
%% none,none}}
%% <0.1.0>: {assoc,{"14400",
%% "EBXqqs44y4MkuvOIVGF+TSUot+/FGqBtMZJm8KyQXwabUc09iB2AesfVb4J2iM2JaPdvk0VgfYur7ywJY9zCZA5bvSNKEOicFP5SAVBsfdCNyCUEjMRt0tvcDAnygWzo",
%% "wBens28gyUh8NzNKVS3IafvWeYE="}}
%% <0.1.0>: {identifier,"=brendonh"}
%% <0.1.0>: {request,{authReq,"https://authn.fullxri.com/authentication/",
%% {2,0},
%% "=brendonh",none}}
%% <0.1.0>: {assoc,{"1800",
%% "AKngKTyiIQ0JcX3/vXrnavfyLWCj6hsiOTYypoKPS25DAaprDRKkq5gXL4q0Foc+YAUqrLlTuT63W6PeVSpZEornRfNHs3Trfoxggp3N4uE8BFvlHvyf1XySXNANPbLLFQ==",
%% "KIW/+jlgDASt3Xx8T7vHfb1F0vU="}}
13 changes: 1 addition & 12 deletions src/openid_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-include("openid.hrl").

%% API
-export([start_link/1, test/0]).
-export([start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
Expand Down Expand Up @@ -265,14 +265,3 @@ check_return_id(ReturnTo, Fields) ->
GivenReturn = ?GV("openid.return_to", Fields),
% XXX Todo -- do this properly, for people who put fancy stuff in return URLs
ReturnTo == GivenReturn.



%%--------------------------------------------------------------------
%%% Tests
%%--------------------------------------------------------------------

test() ->
{ok, Server} = gen_server:start(?MODULE, start_link, [test_server]),
?DBG({server, Server}),
?DBG({one, gen_server:call(Server, {prepare, "foo", "=brendonh", true})}).
45 changes: 1 addition & 44 deletions src/yadis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%%%-------------------------------------------------------------------
-module(yadis).

-export([retrieve/1, test/0]).
-export([retrieve/1]).

-include("openid.hrl").
-include_lib("xmerl/include/xmerl.hrl").
Expand Down Expand Up @@ -155,46 +155,3 @@ get_local_id(Service, [Tag|Rest]) ->
[] -> get_local_id(Service, Rest);
[#xmlElement{content=[Value|_]}|_] -> Value#xmlText.value
end.


%% ------------------------------------------------------------
%% Tests
%% ------------------------------------------------------------

-define(P(S), io:format("~p~n", [S])).

test() ->

?P({"Google:", retrieve("https://www.google.com/accounts/o8/id")}), % Direct XRDS response
%?P({"AOL:", retrieve("https://api.screenname.aol.com/auth/openidServer")}), % x-xrds-location header
?P({"Flickr:", retrieve("http://flickr.com/exbrend")}),
?P({"LiveJournal:", retrieve("http://exbrend.livejournal.com")}), % x-xrds-location meta tag
?P({"XRI Brend:", retrieve("=brendonh")}), % Direct XRDS via xri.net

application:stop(inets). % Avoid error spam from held-open connections


%% $ make test
%% erlc -o ebin -Wall -v +debug_info src/yadis.erl
%% src/yadis.erl:146: Warning: variable 'Rest' is unused
%% erl +W w -pa ebin -noshell -s yadis test -s init stop
%% {"Google:",
%% [{["http://specs.openid.net/auth/2.0/server","http://openid.net/srv/ax/1.0",
%% "http://specs.openid.net/extensions/ui/1.0/mode/popup",
%% "http://specs.openid.net/extensions/ui/1.0/icon",
%% "http://specs.openid.net/extensions/pape/1.0"],
%% ["https://www.google.com/accounts/o8/ud"]}]}
%% {"AOL:",
%% [{["http://specs.openid.net/auth/2.0/return_to"],
%% ["https://api.screenname.aol.com/auth/oidRet"]}]}
%% {"LiveJournal:",
%% [{["http://openid.net/signon/1.0"],
%% ["http://www.livejournal.com/openid/server.bml"]}]}
%% {"XRI Drummond:",
%% [{[],["skype:drummondreed?chat"]},
%% {[],["skype:drummondreed?call"]},
%% {["xri://+i-service*(+forwarding)*($v*1.0)",[]],["http://1id.com/"]},
%% {["http://openid.net/signon/1.0"],
%% ["http://1id.com/sso/","https://1id.com/sso/"]},
%% {["xri://+i-service*(+contact)*($v*1.0)",[]],["http://1id.com/contact/"]}]}

37 changes: 37 additions & 0 deletions test/openid_pm_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-module(openid_pm_test).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").

-define(PL, [{"mode", "error"}, {"error", "This is an example message"}]).
-define(KVF, "mode:error\nerror:This is an example message\n").
-define(URL, "openid.mode=error&openid.error=This%20is%20an%20example%20message").
-define(URI, "openid.mode=error&openid.error=This+is+an+example+message").

url_encode_test_() ->
{"URL Encode", ?_assertEqual(?URL, openid_pm:url_encode(?PL))}.

uri_encode_test_() ->
{"URI Encode", ?_assertEqual(?URI, openid_pm:uri_encode(?PL))}.

kvf_encode_test_() ->
{"Key-Value Form Encode", ?_assertEqual(?KVF, openid_pm:kvf_encode(?PL))}.

kvf_decode_test_() ->
{"Key-Value Form Decode", ?_assertEqual(?PL, openid_pm:kvf_decode(?KVF))}.

prefix_check_test_() ->
Input = [ {"openid." ++ K, V} || {K, V} <- ?PL ],
{"Prefix Check", [?_assertEqual(?KVF, openid_pm:kvf_encode(Input)),
?_assertEqual(?URI, openid_pm:uri_encode(Input))]
}.

types_test_() ->
[ {"Encode " ++ Type,
?_test(begin
Input = [ {Fun(K), Fun(V)} || {K, V} <- ?PL ],
?assertEqual(?KVF, openid_pm:kvf_encode(Input))
end)
} || {Type, Fun} <- [{"Atom", fun erlang:list_to_atom/1},
{"Binary", fun erlang:list_to_binary/1}
] ].

19 changes: 19 additions & 0 deletions test/openid_srv_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(openid_srv_test).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").
-include("openid.hrl").

setup() ->
[ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ].

prepare_test_() ->
{setup, fun setup/0,
{timeout, 5000,
?_test(
begin
{ok, Server} = gen_server:start(openid_srv, start_link, [test_server]),
Result = (catch gen_server:call(Server, {prepare, "foo", "http://exbrend.livejournal.com", true})),
?_assertEqual(ok, element(1, Result))
end)
}
}.
36 changes: 36 additions & 0 deletions test/openid_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-module(openid_test).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").
-include("openid.hrl").

setup() ->
[ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ].

discover2_test_() ->
Cases = [{"https://www.google.com/accounts/o8/id",
#openid_authreq{opURLs = ["https://www.google.com/accounts/o8/ud"],
version = {2,0},
claimedID = "http://specs.openid.net/auth/2.0/identifier_select",
localID = "http://specs.openid.net/auth/2.0/identifier_select",
assoc = none}},
{"http://flickr.com/exbrend",
#openid_authreq{opURLs = ["https://open.login.yahooapis.com/openid/op/auth"],
version = {2,0},
claimedID = "http://specs.openid.net/auth/2.0/identifier_select",
localID = "http://specs.openid.net/auth/2.0/identifier_select",
assoc = none}}
],
{setup, fun setup/0, [?_assertEqual(Result, openid:discover(URL))
|| {URL, Result} <- Cases ]}.

discover1_test_() ->
Cases = [{"blog.paulbonser.com",
#openid_authreq{opURLs = ["http://www.livejournal.com/openid/server.bml"],
version = {1,1},
claimedID = "http://specs.openid.net/auth/2.0/identifier_select",
localID = "http://specs.openid.net/auth/2.0/identifier_select",
assoc = none}}
],
{setup, fun setup/0, [?_assertEqual(Result, openid:discover(URL))
|| {URL, Result} <- Cases ]}.

20 changes: 20 additions & 0 deletions test/openid_utils_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(openid_utils_test).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").

normalization_test_() ->
[?_assertEqual(Result, openid_utils:normalize_id(Id))
|| {Id, Result} <- [{"example.com", "http://example.com/"},
{"http://example.com", "http://example.com/"},
{"https://example.com/", "https://example.com/"},
{"http://example.com/user", "http://example.com/user"},
{"http://example.com/user/", "http://example.com/user/"},
{"http://example.com/", "http://example.com/"},
{"=example", "=example"},
{"xri://=example", "=example"},
{"http://ExAmPlE.CoM/foo/../a", "http://example.com/a"},
{"http://ExAmPlE.CoM/foo/../a/", "http://example.com/a/"},
{"http://eXAMPLE:80//a/./b/../b/%63/%7bfoo%7d", "http://example/a/b/c/%7Bfoo%7D"},
{"http://example//a/b/c/%7Bfoo%7D", "http://example/a/b/c/%7Bfoo%7D"}
]
].
32 changes: 32 additions & 0 deletions test/yadis_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-module(yadis_test).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").
-include("openid.hrl").

setup() ->
[ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ].

retrieve_test_() ->
Cases = [{"https://www.google.com/accounts/o8/id",
#openid_xrds{origID = "https://www.google.com/accounts/o8/id",
claimedID = none, canonicalID = none, isXRI = false,
services = [#openid_xrdservice{types = ["http://specs.openid.net/auth/2.0/server",
"http://openid.net/srv/ax/1.0",
"http://specs.openid.net/extensions/ui/1.0/mode/popup",
"http://specs.openid.net/extensions/ui/1.0/icon",
"http://specs.openid.net/extensions/pape/1.0"],
uris = ["https://www.google.com/accounts/o8/ud"],
localID = none}]}},
{"http://exbrend.livejournal.com",
#openid_xrds{origID = "http://exbrend.livejournal.com",
claimedID = "http://exbrend.livejournal.com/data/yadis",
canonicalID = none,isXRI = false,
services = [#openid_xrdservice{types = ["http://specs.openid.net/auth/2.0/signon"],
uris = ["http://www.livejournal.com/openid/server.bml"],
localID = "http://exbrend.livejournal.com/"}]}},
{"=brendonh",
#openid_xrds{origID = "=brendonh",claimedID = "=brendonh",
canonicalID = none,isXRI = true,services = []}}
],
{setup, fun setup/0, [?_assertEqual(Result, yadis:retrieve(URL))
|| {URL, Result} <- Cases ]}.

0 comments on commit 25598b2

Please sign in to comment.