|
44 | 44 | end |
45 | 45 |
|
46 | 46 | describe "GET /authentication/callback" do |
47 | | - before() do |
48 | | - response = '{"data":{"id":666,"attributes":{"first_name":"Alice","last_name":"Doe","email":"[email protected]","teams":[1,2,3],"role":"Test","tags":[{"key":"city","value":"Paris"}]}}}' |
49 | | - allow(ForestLiana::ForestApiRequester).to receive(:get).with( |
50 | | - "/liana/v2/renderings/42/authorization", { :headers => { "forest-token" => "THE-ACCESS-TOKEN" }, :query => {} } |
51 | | - ).and_return( |
52 | | - instance_double(HTTParty::Response, :body => response, :code => 200) |
53 | | - ) |
54 | | - |
55 | | - get ForestLiana::Engine.routes.url_helpers.authentication_callback_path + "?code=THE-CODE&state=#{CGI::escape('{"renderingId":42}')}" |
56 | | - end |
| 47 | + context 'when the response is a 200' do |
| 48 | + before() do |
| 49 | + response = '{"data":{"id":666,"attributes":{"first_name":"Alice","last_name":"Doe","email":"[email protected]","teams":[1,2,3],"role":"Test","tags":[{"key":"city","value":"Paris"}]}}}' |
| 50 | + allow(ForestLiana::ForestApiRequester).to receive(:get).with( |
| 51 | + "/liana/v2/renderings/42/authorization", { :headers => { "forest-token" => "THE-ACCESS-TOKEN" }, :query => {} } |
| 52 | + ).and_return( |
| 53 | + instance_double(HTTParty::Response, :body => response, :code => 200) |
| 54 | + ) |
57 | 55 |
|
58 | | - it "should respond with a 200 code" do |
59 | | - expect(response).to have_http_status(200) |
60 | | - end |
| 56 | + get ForestLiana::Engine.routes.url_helpers.authentication_callback_path + "?code=THE-CODE&state=#{CGI::escape('{"renderingId":42}')}" |
| 57 | + end |
61 | 58 |
|
62 | | - it "should return a valid authentication token" do |
63 | | - body = JSON.parse(response.body, :symbolize_names => true); |
| 59 | + it "should respond with a 200 code" do |
| 60 | + expect(response).to have_http_status(200) |
| 61 | + end |
64 | 62 |
|
65 | | - token = body[:token] |
66 | | - decoded = JWT.decode(token, ForestLiana.auth_secret, true, { algorithm: 'HS256' })[0] |
| 63 | + it "should return a valid authentication token" do |
| 64 | + body = JSON.parse(response.body, :symbolize_names => true); |
67 | 65 |
|
68 | | - expected_token_data = { |
69 | | - "id" => 666, |
70 | | - |
71 | | - "rendering_id" => "42", |
72 | | - "first_name" => 'Alice', |
73 | | - "last_name" => 'Doe', |
74 | | - "team" => 1, |
75 | | - "role" => "Test", |
76 | | - } |
| 66 | + token = body[:token] |
| 67 | + decoded = JWT.decode(token, ForestLiana.auth_secret, true, { algorithm: 'HS256' })[0] |
77 | 68 |
|
78 | | - expect(decoded).to include(expected_token_data) |
79 | | - tags = decoded['tags'] |
80 | | - expect(tags.length).to eq(1) |
81 | | - expect(tags[0]['key']).to eq("city") |
82 | | - expect(tags[0]['value']).to eq("Paris") |
83 | | - expect(body).to eq({ token: token, tokenData: decoded.deep_symbolize_keys! }) |
84 | | - expect(response).to have_http_status(200) |
| 69 | + expected_token_data = { |
| 70 | + "id" => 666, |
| 71 | + |
| 72 | + "rendering_id" => "42", |
| 73 | + "first_name" => 'Alice', |
| 74 | + "last_name" => 'Doe', |
| 75 | + "team" => 1, |
| 76 | + "role" => "Test", |
| 77 | + } |
| 78 | + |
| 79 | + expect(decoded).to include(expected_token_data) |
| 80 | + tags = decoded['tags'] |
| 81 | + expect(tags.length).to eq(1) |
| 82 | + expect(tags[0]['key']).to eq("city") |
| 83 | + expect(tags[0]['value']).to eq("Paris") |
| 84 | + expect(body).to eq({ token: token, tokenData: decoded.deep_symbolize_keys! }) |
| 85 | + expect(response).to have_http_status(200) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context 'when the response is not a 200' do |
| 90 | + before() do |
| 91 | + get ForestLiana::Engine.routes.url_helpers.authentication_callback_path, |
| 92 | + params: { |
| 93 | + error: 'TrialBlockedError', |
| 94 | + error_description: 'Your free trial has ended. We hope you enjoyed your experience with Forest Admin.', |
| 95 | + state: '{"renderingId":100}' |
| 96 | + }, |
| 97 | + headers: { |
| 98 | + 'Accept' => 'application/json', |
| 99 | + 'Content-Type' => 'application/json', |
| 100 | + } |
| 101 | + end |
| 102 | + |
| 103 | + it "should respond with a 401 code" do |
| 104 | + expect(response).to have_http_status(401) |
| 105 | + expect(response.body).to eq('{"error":"TrialBlockedError","error_description":"Your free trial has ended. We hope you enjoyed your experience with Forest Admin.","state":"{\"renderingId\":100}"}') |
| 106 | + end |
85 | 107 | end |
86 | 108 | end |
87 | 109 |
|
|
0 commit comments