From 6dbf3801a81b1fdf1d798f426ad1343f4ee268fd Mon Sep 17 00:00:00 2001 From: Mauricio Cassola Date: Fri, 9 Sep 2022 14:40:29 -0300 Subject: [PATCH 1/3] [MER-879] Do not derive id --- lib/lti_1p3/tool/services/ags/line_item.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lti_1p3/tool/services/ags/line_item.ex b/lib/lti_1p3/tool/services/ags/line_item.ex index c13c2fa..5a5887b 100644 --- a/lib/lti_1p3/tool/services/ags/line_item.ex +++ b/lib/lti_1p3/tool/services/ags/line_item.ex @@ -1,5 +1,5 @@ defmodule Lti_1p3.Tool.Services.AGS.LineItem do - @derive Jason.Encoder + @derive {Jason.Encoder, except: [:id]} @enforce_keys [:scoreMaximum, :label, :resourceId] defstruct [:id, :scoreMaximum, :label, :resourceId] From 9f58ca9268cb571c0a2e2073b782e5c3477563ad Mon Sep 17 00:00:00 2001 From: Mauricio Cassola Date: Fri, 9 Sep 2022 14:41:00 -0300 Subject: [PATCH 2/3] [MER-879] Add missing membership accept header --- lib/lti_1p3/tool/services/nrps.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lti_1p3/tool/services/nrps.ex b/lib/lti_1p3/tool/services/nrps.ex index 519e379..0f9f900 100644 --- a/lib/lti_1p3/tool/services/nrps.ex +++ b/lib/lti_1p3/tool/services/nrps.ex @@ -76,7 +76,8 @@ defmodule Lti_1p3.Tool.Services.NRPS do defp headers(%AccessToken{} = access_token) do [ {"Content-Type", "application/json"}, - {"Authorization", "Bearer #{access_token.access_token}"} + {"Authorization", "Bearer #{access_token.access_token}"}, + {"Accept", "application/vnd.ims.lti-nrps.v2.membershipcontainer+json"} ] end end From 6b68ee5c3a8c4c2b95359e4c38656e08ca5a2bb2 Mon Sep 17 00:00:00 2001 From: Mauricio Cassola Date: Fri, 9 Sep 2022 14:53:23 -0300 Subject: [PATCH 3/3] [MER-879] Add some tests --- test/lti_1p3/tool/services/ags_test.exs | 2 +- test/lti_1p3/tool/services/nrps_test.exs | 53 +++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/test/lti_1p3/tool/services/ags_test.exs b/test/lti_1p3/tool/services/ags_test.exs index 2e0b683..ed6472c 100644 --- a/test/lti_1p3/tool/services/ags_test.exs +++ b/test/lti_1p3/tool/services/ags_test.exs @@ -412,7 +412,7 @@ defmodule Lti_1p3.Tool.Services.AGSTest do ) end - test "fetch line item set headers correctly", %{ + test "fetch line item is build correctly with query params", %{ access_token: access_token } do expect(MockHTTPoison, :get, fn url, _headers -> diff --git a/test/lti_1p3/tool/services/nrps_test.exs b/test/lti_1p3/tool/services/nrps_test.exs index ac91d62..e16324e 100644 --- a/test/lti_1p3/tool/services/nrps_test.exs +++ b/test/lti_1p3/tool/services/nrps_test.exs @@ -1,7 +1,12 @@ defmodule Lti_1p3.Tool.Services.NRPSTest do use ExUnit.Case, async: true - alias Lti_1p3.Tool.Services.NRPS + import Mox + + alias Lti_1p3.Test.MockHTTPoison + alias Lti_1p3.Tool.Services.{AccessToken, NRPS} + + @context_memberships_url "https://lms.example.edu/api/lti/courses/8/names_and_roles" @lti_params %{ "aud" => "10000000000041", @@ -23,7 +28,7 @@ defmodule Lti_1p3.Tool.Services.NRPSTest do "validation_context" => nil }, "https://purl.imsglobal.org/spec/lti-nrps/claim/namesroleservice" => %{ - "context_memberships_url" => "https://lms.example.edu/api/lti/courses/8/names_and_roles", + "context_memberships_url" => @context_memberships_url, "service_versions" => ["2.0"] }, "https://purl.imsglobal.org/spec/lti/claim/context" => %{ @@ -88,10 +93,46 @@ defmodule Lti_1p3.Tool.Services.NRPSTest do "sub" => "c36c3c87-993f-4d2e-9e22-e47d5d2637ae" } - test "nrps" do - assert NRPS.nrps_enabled?(@lti_params) + describe "nrps" do + setup [:setup_session] + + test "access to lti params is correct" do + assert NRPS.nrps_enabled?(@lti_params) + + assert NRPS.get_context_memberships_url(@lti_params) == + @context_memberships_url + end + + test "nrps fetch memberships set headers correctly", %{ + access_token: access_token + } do + expect(MockHTTPoison, :get, fn _url, headers -> + assert [ + {"Content-Type", "application/json"}, + {"Authorization", "Bearer fake_token"}, + {"Accept", "application/vnd.ims.lti-nrps.v2.membershipcontainer+json"} + ] == headers + + {:ok, %HTTPoison.Response{status_code: 200, body: "{\"members\": []}"}} + end) + + {:ok, _response} = + NRPS.fetch_memberships( + @context_memberships_url, + access_token + ) + end + end + + defp setup_session(_context) do + access_token = %AccessToken{ + scope: + "https://purl.imsglobal.org/spec/lti-ags/scope/score https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly", + access_token: "fake_token", + token_type: "Bearer", + expires_in: 3_600 + } - assert NRPS.get_context_memberships_url(@lti_params) == - "https://lms.example.edu/api/lti/courses/8/names_and_roles" + {:ok, %{access_token: access_token}} end end