Skip to content

Commit

Permalink
Update GetWebSessionRequest to support masquerading
Browse files Browse the repository at this point in the history
refs: MBL-17910
affects: Student, Teacher, Parent
release note: none

test plan: See PR description
  • Loading branch information
rh12 committed Oct 30, 2024
1 parent 66815c5 commit dde191a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class K5SubjectViewMasqueradedSession {

private func startMasqueradedSession(for url: URL, itemIndex: Int) {
masqueradedSessionRequest?.cancel()
masqueradedSessionRequest = env.api.makeRequest(GetWebSessionRequest(to: url, path: "login/session_token")) { [weak self] response, _, _ in
masqueradedSessionRequest = env.api.makeRequest(GetWebSessionRequest(to: url)) { [weak self] response, _, _ in
performUIUpdate {
let safeURL = response?.session_url ?? url
self?.masqueradedSessionURLSubject.send(safeURL)
Expand Down
29 changes: 23 additions & 6 deletions Core/Core/Login/APIOAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,36 @@ public struct GetWebSessionRequest: APIRequestable {
public let path: String

public var query: [APIQueryItem] {
guard let to else { return [] }

// Inline data content URLs need no extra query params
if let to = to, to.scheme == "data" {
if to.scheme == "data" {
return [ .value("return_to", to.absoluteString) ]
}

if let returnTo = to?.appendingQueryItems(URLQueryItem(name: "display", value: "borderless")) {
return [ .value("return_to", returnTo.absoluteString) ]
var returnToUrlQueryItems = [
URLQueryItem(name: "display", value: "borderless")
]

if let actAsUserID = AppEnvironment.shared.currentSession?.actAsUserID {
returnToUrlQueryItems.append(URLQueryItem(name: "as_user_id", value: actAsUserID))
}

var returnToUrl = to
returnToUrlQueryItems.forEach {
returnToUrl = returnToUrl.appendingQueryItems($0)
}
return []
return [ .value("return_to", returnToUrl.absoluteString) ]
}

public init(to: URL?, path: String = "/login/session_token") {
public init(to: URL?) {
self.to = to
self.path = path

let isMasqueareding = AppEnvironment.shared.currentSession?.masquerader != nil
if isMasqueareding {
self.path = "/api/v1/login/session_token"
} else {
self.path = "/login/session_token"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class K5SubjectViewMasqueradedSessionTests: CoreTestCase {

func testFetchesSessionURL() {
let sessionExpectation = expectation(description: "Session fetched from API")
let request = GetWebSessionRequest(to: URL(string: "/first_tab_url")!, path: "login/session_token")
let request = GetWebSessionRequest(to: URL(string: "/first_tab_url")!)
api.mock(request, value: .init(session_url: URL(string: "/session_url")!, requires_terms_acceptance: false))

let testee = K5SubjectViewMasqueradedSession(env: environment)
Expand Down
18 changes: 16 additions & 2 deletions Core/CoreTests/Login/APIOAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
//

import XCTest
import TestsFoundation
@testable import Core

class APIOAuthTests: XCTestCase {
class APIOAuthTests: CoreTestCase {
func testGetMobileVerifyRequest() {
XCTAssertEqual(GetMobileVerifyRequest(domain: "cgnu").path, "https://canvas.instructure.com/api/v1/mobile_verify.json")
XCTAssertEqual(GetMobileVerifyRequest(domain: "cgnu").queryItems, [
Expand Down Expand Up @@ -61,11 +62,24 @@ class APIOAuthTests: XCTestCase {
XCTAssertEqual(DeleteLoginOAuthRequest().path, "/login/oauth2/token")
}

func testGetWebSessionRequest() {
func testGetWebSessionRequestWhenNotMasquerading() {
XCTAssertEqual(GetWebSessionRequest(to: nil).path, "/login/session_token")
XCTAssertEqual(GetWebSessionRequest(to: nil).queryItems, [])
XCTAssertEqual(GetWebSessionRequest(to: URL(string: "/")).queryItems, [
URLQueryItem(name: "return_to", value: "/?display=borderless")
])
}

func testGetWebSessionRequestWhenMasquerading() {
environment.currentSession = .make(
masquerader: URL(string: "something"),
userID: "42"
)

XCTAssertEqual(GetWebSessionRequest(to: nil).path, "/api/v1/login/session_token")
XCTAssertEqual(GetWebSessionRequest(to: nil).queryItems, [])
XCTAssertEqual(GetWebSessionRequest(to: URL(string: "/")).queryItems, [
URLQueryItem(name: "return_to", value: "/?display=borderless&as_user_id=42"),
])
}
}

0 comments on commit dde191a

Please sign in to comment.