anchor.diff:      ok.
   
   malformed_request_test() ->
  -    MeckModules = [cowboy_req, oidcc],
  +    {ok, Meck} = start_meck(),
  +    State = #state{},
  +    Requests = [
  +                % GOOD request
  +                {#{ bindings => [{version, <<"v1">>},
  +                                 {type, <<"oidcp">>}
  +                                ],
  +                    header => [],
  +                    method => <<"GET">>,
  +                    body => []
  +                  }, false },
  +
  +                { #{ bindings => [{version, <<"V1">>},
  +                                  {type, <<"service">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, false},
  +
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, false},
  +
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential_data">>},
  +                                  {id, <<"234">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, false },
  +
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential">>}
  +                                 ],
  +                     header => [{<<"content-type">>,{ok, {<<"application">>,
  +                                                           <<"json">>,[]}}}],
  +                     method => <<"POST">>,
  +                     body => <<"{\"service_id\":\"234\"}">>
  +                   }, false },
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential">>},
  +                                  {id, <<"234">>}
  +                                 ],
  +                     header => [
  +                                {<<"authorization">>,<<"Bearer SomeToken">>},
  +                                {<<"x-openid-connect-issuer">>,?ISSUER_URL}
  +                               ],
  +                     method => <<"DELETE">>,
  +                     body => []
  +                   }, false },
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential">>},
  +                                  {id, <<"234">>}
  +                                 ],
  +                     header => [
  +                                {<<"authorization">>,<<"Bearer SomeToken">>},
  +                                {<<"x-openid-connect-issuer">>,<<"ID1">>}
  +                               ],
  +                     method => <<"DELETE">>,
  +                     body => []
  +                   }, false },
  +
  +                % BAD requests
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"credential">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"POST">>,
  +                     body => <<"{\"service_id\":234}">>
  +                   }, true },
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"oidcp">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"POST">>,
  +                     body => <<"no json">>
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"latest">>},
  +                                  {type, <<"unknown_type">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"v0">>},
  +                                  {type, <<"oidcp">>}
  +                                 ],
  +                     header => [{<<"authorization">>, <<"missingBearer">>}],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"v0">>},
  +                                  {type, <<"credentials">>}
  +                                 ],
  +                     header => [
  +                                {<<"authorization">>,<<"Bearer SomeToken">>},
  +                                {<<"x-openid-connect-issuer">>,<<"NO URL">>}
  +                               ],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"v0">>},
  +                                  {type, <<"credentials">>}
  +                                 ],
  +                     header => [
  +                                {<<"authorization">>,<<"Bearer SomeToken">>},
  +                                {<<"x-openid-connect-issuer">>,<<"ID2">>}
  +                               ],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"vn">>},
  +                                  {type, <<"oidcp">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true },
  +
  +                { #{ bindings => [{version, <<"234">>},
  +                                  {type, <<"oidcp">>}
  +                                 ],
  +                     header => [],
  +                     method => <<"GET">>,
  +                     body => []
  +                   }, true }
  +               ],
  +
  +    Test  = fun({Request, ExpResult}, _) ->
  +                    io:format("testing with request ~p~n",[Request]),
  +                    {Result, Request, _} = tts_rest:malformed_request(Request,
  +                                                                      State),
  +                    io:format("got result ~p, expecting ~p~n",[Result, ExpResult]),
  +                    ?assertEqual(ExpResult, Result),
  +                    ok
  +            end,
  +    ok = lists:foldl(Test,ok,Requests),
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +is_authorized_test() ->
  +    {ok, Meck} = start_meck(),
  +    Authz = <<"authorization">>,
  +    Mapping = [
  +
  +               {#state{}, {false, <<"authorization">>}},
  +               {#state{type=some_unknown, token=defined}, {false, Authz}},
  +               {#state{type=service}, {false, <<"authorization">>}},
  +               {#state{type=service, issuer= <<"issuer">>}, {false, Authz}},
  +               {#state{type=service, issuer= <<"issuer">>, token= <<"token">>}, {false, Authz}},
  +               {#state{type=some_service, issuer= <<"issuer">>, token= <<"token">>}, {false, Authz}},
  +
  +               {#state{type=oidcp}, true},
  +               {#state{type=info}, true},
  +               {#state{type=logout}, true},
  +               {#state{type=service, issuer= <<"issuer">>, token= <<"token">>}, {false, Authz}},
  +               {#state{type=service, issuer= <<"unknown">>, token= <<"token">>}, {false, Authz}},
  +               {#state{type=service, issuer= <<"known">>, token= <<"token">>}, {false, Authz}},
  +               {#state{type=service, issuer= <<"known">>, token= <<"good1">>}, true},
  +               {#state{type=service, session_pid=self()}, {false, Authz}},
  +               {#state{type=unknown, session_pid=self()}, {false, Authz}},
  +               {#state{type=service, issuer= <<"known">>, token= <<"good2">>}, true}
  +              ],
  +    Req = req,
  +    Test = fun({State, ExpResult}, _AccIn) ->
  +                   io:format("testing with state ~p~n",[State]),
  +                   {Result, Req, _CState} = tts_rest:is_authorized(Req, State),
  +                   io:format("got result ~p, expecting ~p~n",[Result, ExpResult]),
  +                   ?assertEqual(ExpResult, Result),
  +                   ok
  +           end,
  +    ok = lists:foldl(Test, ok, Mapping),
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +
  +resource_exists_test() ->
  +    {ok, Meck} = start_meck(),
  +
  +    Requests = [
  +                %% good requests
  +                {#state{id = undefined}, false},
  +                {#state{id = undefined, type=oidcp}, true},
  +                {#state{id = undefined, type=service}, true},
  +                {#state{id = undefined, type=credential}, true},
  +                {#state{id = <<"123">>, type=credential}, true},
  +                {#state{type = cred_data, id= <<"someid">>}, true},
  +
  +                %% bad requests
  +                {#state{id = <<"123">>, type=oidcp}, false},
  +                {#state{id = <<"123">>, type=service}, false},
  +                {#state{id = <<"124">>, type=oidcp}, false},
  +                {#state{id = <<"124">>, type=service}, false},
  +                {#state{type = creddata, id= <<"someid">>}, false},
  +                {#state{type = creddata }, false}
  +
  +               ],
  +
  +    Test  = fun({State, ExpResult}, _) ->
  +                    io:format("testing ~p, expecting: ~p~n", [State, ExpResult]),
  +                    {Result, req, _} = tts_rest:resource_exists(req, State),
  +                    ?assertEqual(ExpResult, Result),
  +                    ok
  +            end,
  +    ok = lists:foldl(Test,ok,Requests),
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +get_json_test() ->
  +    {ok, Meck} = start_meck(),
  +    Requests = [
  +                {#state{version = latest,
  +                        type = service,
  +                        id = undefined,
  +                        session_pid = pid1,
  +                        method = get
  +                       }, <<"{\"service_list\":[{}]}">> },
  +                {#state{version = latest,
  +                        type = service,
  +                        id = undefined,
  +                        session_pid = pid2,
  +                        method = get
  +                       }, <<"{\"service_list\":[]}">> },
  +                {#state{version = latest,
  +                        type = oidcp,
  +                        id = undefined,
  +                        session_pid = undefined,
  +                        method = get
  +                       },<<"{\"openid_provider_list\":[{\"id\":\"ID1\",\"issuer\":\"https:\\/\\/test.tts\",\"ready\":true},{\"id\":\"ID2\",\"issuer\":\"https:\\/\\/other.tts\",\"ready\":false}]}">>
  +                },
  +                {#state{version = latest,
  +                        type = cred_data,
  +                        id = <<"CRED1">>,
  +                        session_pid = pid1,
  +                        method = get
  +                       },<<"{\"password\":\"secret\"}">> },
  +                {#state{version = latest,
  +                        type = cred_data,
  +                        id = <<"CRED1">>,
  +                        session_pid = pid2,
  +                        method = get
  +                       },<<"{\"result\":\"error\",\"user_msg\":\"Sorry, the requested data was not found\"}">> }
  +               ],
  +
  +    Test  = fun({State, ExpResult}, _) ->
  +                    io:format("Expecting ~p on state ~p~n",[ExpResult, State]),
  +                    {Result, req, _NewState} = tts_rest:get_json(req, State),
  +                    ?assertEqual(ExpResult, Result),
  +                    ok
  +            end,
  +    ok = lists:foldl(Test,ok,Requests),
  +
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +post_json_test() ->
  +    {ok, Meck} = start_meck(),
  +    application:set_env(tts, ep_main, <<"/">>),
  +
  +    Url = <<"/api/v2/credential_data/CRED1">>,
  +    Requests = [
  +                {#state{version = 2,
  +                        type = credential,
  +                        id = undefined,
  +                        json = #{service_id => <<"Service1">>},
  +                        session_pid = pid1,
  +                        method = post
  +                       }, {true, Url} },
  +                {#state{version = 2,
  +                        type = credential,
  +                        id = undefined,
  +                        json = #{service_id => <<"Service1">>},
  +                        session_pid = pid2,
  +                        method = post
  +                       }, false }
  +               ],
  +
  +    Test  = fun({State, ExpResult}, _) ->
  +                    io:format("Expecting ~p on state ~p~n",[ExpResult, State]),
  +                    {Result, req, _NewState} = tts_rest:post_json(req, State),
  +                    ?assertEqual(ExpResult, Result),
  +                    ok
  +            end,
  +    ok = lists:foldl(Test,ok,Requests),
  +    %% ok = test_util:meck_done(MeckModules),
  +    application:unset_env(tts, ep_main),
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +delete_resource_test() ->
  +    {ok, Meck} = start_meck(),
  +
  +    Requests = [
  +                {#state{version = latest,
  +                        type = credential,
  +                        id = <<"CRED1">>,
  +                        session_pid = pid1,
  +                        method = delete
  +                       }, true },
  +                {#state{version = latest,
  +                        type = credential,
  +                        id = <<"CRED1">>,
  +                        session_pid = pid2,
  +                        method = delete
  +                       }, false }
  +               ],
  +
  +    Test  = fun({State, ExpResult}, _) ->
  +                    io:format("Expecting ~p on state ~p~n",[ExpResult, State]),
  +                    {Result, req, _NewState} = tts_rest:delete_resource(req, State),
  +                    ?assertEqual(ExpResult, Result),
  +                    ok
  +            end,
  +    ok = lists:foldl(Test,ok,Requests),
  +    ok = stop_meck(Meck),
  +    ok.
  +
  +
  +start_meck() ->
  +    MeckModules = [cowboy_req, oidcc, tts, tts_session],
       Binding = fun(Name, #{bindings := Bindings} = Request, Default) ->
                         case lists:keyfind(Name, 1, Bindings) of
                             {Name, Value} -> {Value, Request};
  
positive.code:                               _ -> false
                            end
                end,
  
  
      ok = test_util:meck_new(MeckModules),
      ok = meck:expect(tts, does_credential_exist, CredExists),
      ok = meck:expect(tts, does_temp_cred_exist, CredDataExists),
      ok = meck:expect(cowboy_req, set_resp_body, fun(_, Req) -> Req end),
  
      Requests = [
                  %% good requests
                  {#state{id = undefined}, false},
                  {#state{id = undefined, type=oidcp}, true},
                  {#state{id = undefined, type=service}, true},
                  {#state{id = undefined, type=credential}, true},
                  {#state{id = <<"123">>, type=credential}, true},
                  {#state{type = cred_data, id= <<"someid">>}, true},
  
                  %% bad requests
                  {#state{id = <<"123">>, type=oidcp}, false},
                  {#state{id = <<"123">>, type=service}, false},
                  {#state{id = <<"124">>, type=oidcp}, false},
                  {#state{id = <<"124">>, type=service}, false},
                  {#state{type = creddata, id= <<"someid">>}, false},
                  {#state{type = creddata }, false}
  
                 ],
  
      Test  = fun({State, ExpResult}, _) ->
                      io:format("testing ~p, expecting: ~p~n", [State, ExpResult]),
                      {Result, req, _} = tts_rest:resource_exists(req, State),
                      ?assertEqual(ExpResult, Result),
                      ok
              end,
      ok = lists:foldl(Test,ok,Requests),
      ok = test_util:meck_done(MeckModules),
      ok.
  
  get_json_test() ->
      MeckModules = [tts],
  
      GetProvider = fun() ->
                            {ok, [#{id => <<"ID1">>,
  
negative.code:          }).
  
  rest_init_test() ->
      {ok, Meck} = start_meck(),
      Req = req,
      {ok, Req, #state{}} = tts_rest:rest_init(Req, doesnt_matter),
      ok = stop_meck(Meck),
      ok.
  
  allowed_methods_test() ->
  