|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe API::Reporting::OneTimeTokensController do |
| 6 | + let(:user) { create(:user) } |
| 7 | + let(:mock_cis2_info) { { "some_key" => "some value" } } |
| 8 | + let(:valid_token) do |
| 9 | + ReportingAPI::OneTimeToken.find_or_generate_for!( |
| 10 | + user:, |
| 11 | + cis2_info: mock_cis2_info |
| 12 | + ) |
| 13 | + end |
| 14 | + let(:invalid_token) { SecureRandom.hex(32) } |
| 15 | + |
| 16 | + describe "#authorize" do |
| 17 | + context "given a valid client_id when reporting_api is enabled" do |
| 18 | + before { Flipper.enable(:reporting_api) } |
| 19 | + |
| 20 | + let(:client_id) { Settings.reporting_api.client_app.client_id } |
| 21 | + let(:grant_type) { "some_grant_type" } |
| 22 | + |
| 23 | + let(:do_the_request) do |
| 24 | + post :authorize, |
| 25 | + params: { |
| 26 | + code: token.token, |
| 27 | + grant_type: grant_type, |
| 28 | + client_id: client_id |
| 29 | + }, |
| 30 | + format: :json |
| 31 | + end |
| 32 | + |
| 33 | + context "and a valid OneTimeToken in the code param" do |
| 34 | + let(:token) { valid_token } |
| 35 | + |
| 36 | + context "and a grant_type which is not authorization_code" do |
| 37 | + let(:grant_type) { "not_an_authorization_code" } |
| 38 | + |
| 39 | + it "responds with HTTP 400" do |
| 40 | + do_the_request |
| 41 | + expect(response.status).to eq(400) |
| 42 | + end |
| 43 | + |
| 44 | + describe "the response json" do |
| 45 | + let(:response_json) { JSON.parse(response.body) } |
| 46 | + |
| 47 | + it "has an error key set to unsupported_grant_type" do |
| 48 | + do_the_request |
| 49 | + expect(response_json["error"]).to eq("unsupported_grant_type") |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "and a grant_type of authorization_code" do |
| 56 | + # this param name and value is required by the OAUTH 2.0 spec |
| 57 | + # see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3 |
| 58 | + let(:grant_type) { "authorization_code" } |
| 59 | + |
| 60 | + context "and a valid OneTimeToken in the code param" do |
| 61 | + let(:token) { valid_token } |
| 62 | + |
| 63 | + it "responds with 200" do |
| 64 | + do_the_request |
| 65 | + expect(response.status).to eq(200) |
| 66 | + end |
| 67 | + |
| 68 | + it "deletes the OneTimeToken" do |
| 69 | + do_the_request |
| 70 | + expect(ReportingAPI::OneTimeToken.exists?(token.id)).to be(false) |
| 71 | + end |
| 72 | + |
| 73 | + it "responds with json" do |
| 74 | + do_the_request |
| 75 | + expect(response.headers["Content-type"]).to eq( |
| 76 | + "application/json; charset=utf-8" |
| 77 | + ) |
| 78 | + end |
| 79 | + |
| 80 | + describe "the response json" do |
| 81 | + let(:response_json) { JSON.parse(response.body) } |
| 82 | + |
| 83 | + it "includes a JWT" do |
| 84 | + do_the_request |
| 85 | + expect(response_json["jwt"]).not_to be_empty |
| 86 | + end |
| 87 | + |
| 88 | + describe "the JWT payload" do |
| 89 | + let(:payload) { response_json["jwt"] } |
| 90 | + |
| 91 | + it "is encoded with the Mavis reporting app secret" do |
| 92 | + do_the_request |
| 93 | + expect { |
| 94 | + JWT.decode( |
| 95 | + response_json["jwt"], |
| 96 | + Settings.reporting_api.client_app.secret, |
| 97 | + true, |
| 98 | + { algorithm: "HS512" } |
| 99 | + ) |
| 100 | + }.not_to raise_error |
| 101 | + end |
| 102 | + |
| 103 | + describe "once decoded" do |
| 104 | + let(:decoded_payload) do |
| 105 | + JWT.decode( |
| 106 | + response_json["jwt"], |
| 107 | + Settings.reporting_api.client_app.secret, |
| 108 | + true, |
| 109 | + { algorithm: "HS512" } |
| 110 | + ) |
| 111 | + end |
| 112 | + let(:jwt_data) { decoded_payload.first["data"] } |
| 113 | + |
| 114 | + it "includes the user attributes" do |
| 115 | + do_the_request |
| 116 | + expect(jwt_data["user"]).to eq(user.as_json) |
| 117 | + end |
| 118 | + |
| 119 | + it "includes the users cis2_info" do |
| 120 | + do_the_request |
| 121 | + expect(jwt_data["cis2_info"]).to eq(mock_cis2_info) |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + context "and a OneTimeToken in the code param which can't be found in the users table" do |
| 129 | + let(:do_the_request) do |
| 130 | + post :authorize, |
| 131 | + params: { |
| 132 | + code: invalid_token, |
| 133 | + grant_type: grant_type, |
| 134 | + client_id: client_id |
| 135 | + }, |
| 136 | + format: :json |
| 137 | + end |
| 138 | + let(:response_json) { JSON.parse(response.body) } |
| 139 | + |
| 140 | + it "returns 403" do |
| 141 | + do_the_request |
| 142 | + expect(response.status).to eq(403) |
| 143 | + end |
| 144 | + |
| 145 | + it "returns an error in the body" do |
| 146 | + do_the_request |
| 147 | + expect(response_json).to eq({ "errors" => "invalid_grant" }) |
| 148 | + end |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments