diff options
| author | Egor Kislitsyn <egor@kislitsyn.com> | 2019-05-07 16:51:11 +0700 | 
|---|---|---|
| committer | Egor Kislitsyn <egor@kislitsyn.com> | 2019-05-07 16:51:11 +0700 | 
| commit | 1557b99beb3b406572ef2d3baaabed1c9baeca1c (patch) | |
| tree | 34041b8526ecfa38fcb0b471718de0b3b71aa8c4 /test/web/oauth | |
| parent | c157e27a000a12dc8f660c056744a6611beb01b1 (diff) | |
| parent | 6518644db1d31f2b30b95fa0899b3751bc330d56 (diff) | |
| download | pleroma-1557b99beb3b406572ef2d3baaabed1c9baeca1c.tar.gz pleroma-1557b99beb3b406572ef2d3baaabed1c9baeca1c.zip | |
Merge remote-tracking branch 'pleroma/develop' into feature/disable-account
Diffstat (limited to 'test/web/oauth')
| -rw-r--r-- | test/web/oauth/oauth_controller_test.exs | 196 | 
1 files changed, 196 insertions, 0 deletions
| diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index 6e96537ec..cb6836983 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -12,6 +12,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do    alias Pleroma.Web.OAuth.Authorization    alias Pleroma.Web.OAuth.Token +  @oauth_config_path [:oauth2, :issue_new_refresh_token]    @session_opts [      store: :cookie,      key: "_test", @@ -714,4 +715,199 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do        refute Map.has_key?(resp, "access_token")      end    end + +  describe "POST /oauth/token - refresh token" do +    setup do +      oauth_token_config = Pleroma.Config.get(@oauth_config_path) + +      on_exit(fn -> +        Pleroma.Config.get(@oauth_config_path, oauth_token_config) +      end) +    end + +    test "issues a new access token with keep fresh token" do +      Pleroma.Config.put(@oauth_config_path, true) +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      new_token = Repo.get_by(Token, token: response["access_token"]) +      assert new_token.refresh_token == token.refresh_token +      assert new_token.scopes == auth.scopes +      assert new_token.user_id == user.id +      assert new_token.app_id == app.id +    end + +    test "issues a new access token with new fresh token" do +      Pleroma.Config.put(@oauth_config_path, false) +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      new_token = Repo.get_by(Token, token: response["access_token"]) +      refute new_token.refresh_token == token.refresh_token +      assert new_token.scopes == auth.scopes +      assert new_token.user_id == user.id +      assert new_token.app_id == app.id +    end + +    test "returns 400 if we try use access token" do +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(400) + +      assert %{"error" => "Invalid credentials"} == response +    end + +    test "returns 400 if refresh_token invalid" do +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => "token.refresh_token", +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(400) + +      assert %{"error" => "Invalid credentials"} == response +    end + +    test "issues a new token if token expired" do +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      change = +        Ecto.Changeset.change( +          token, +          %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)} +        ) + +      {:ok, access_token} = Repo.update(change) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => access_token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      token = Repo.get_by(Token, token: response["access_token"]) +      assert token +      assert token.scopes == auth.scopes +      assert token.user_id == user.id +      assert token.app_id == app.id +    end +  end + +  describe "POST /oauth/token - bad request" do +    test "returns 500" do +      response = +        build_conn() +        |> post("/oauth/token", %{}) +        |> json_response(500) + +      assert %{"error" => "Bad request"} == response +    end +  end + +  describe "POST /oauth/revoke - bad request" do +    test "returns 500" do +      response = +        build_conn() +        |> post("/oauth/revoke", %{}) +        |> json_response(500) + +      assert %{"error" => "Bad request"} == response +    end +  end  end | 
