|
| 1 | +describe("PoEAPI auth", function() |
| 2 | + local originalLaunchSubScript |
| 3 | + local originalDownloadPage |
| 4 | + local originalSubScripts |
| 5 | + |
| 6 | + before_each(function() |
| 7 | + originalLaunchSubScript = _G.LaunchSubScript |
| 8 | + originalDownloadPage = launch.DownloadPage |
| 9 | + originalSubScripts = launch.subScripts |
| 10 | + launch.subScripts = { } |
| 11 | + end) |
| 12 | + |
| 13 | + after_each(function() |
| 14 | + _G.LaunchSubScript = originalLaunchSubScript |
| 15 | + launch.DownloadPage = originalDownloadPage |
| 16 | + launch.subScripts = originalSubScripts |
| 17 | + end) |
| 18 | + |
| 19 | + it("passes token exchange errors to the auth callback #auth", function() |
| 20 | + local authState |
| 21 | + _G.LaunchSubScript = function(_, _, _, authUrl) |
| 22 | + authState = authUrl:match("state=([^&]+)") |
| 23 | + return 123 |
| 24 | + end |
| 25 | + launch.DownloadPage = function(_, url, callback) |
| 26 | + assert.are.equals("https://www.pathofexile.com/oauth/token", url) |
| 27 | + callback(nil, "SSL connect error") |
| 28 | + end |
| 29 | + |
| 30 | + local api = new("PoEAPI") |
| 31 | + local callbackArgs |
| 32 | + api:FetchAuthToken(function(response, errMsg, updateSettings) |
| 33 | + callbackArgs = { |
| 34 | + response = response, |
| 35 | + errMsg = errMsg, |
| 36 | + updateSettings = updateSettings, |
| 37 | + } |
| 38 | + end) |
| 39 | + |
| 40 | + assert.is_not_nil(authState) |
| 41 | + assert.is_not_nil(launch.subScripts[123]) |
| 42 | + launch.subScripts[123].callback("auth-code", nil, authState, 12345) |
| 43 | + |
| 44 | + assert.is_nil(callbackArgs.response) |
| 45 | + assert.are.equals("SSL connect error", callbackArgs.errMsg) |
| 46 | + assert.True(callbackArgs.updateSettings) |
| 47 | + assert.is_nil(api.authToken) |
| 48 | + end) |
| 49 | + |
| 50 | + it("reports OAuth state mismatches without exchanging a token", function() |
| 51 | + _G.LaunchSubScript = function() |
| 52 | + return 123 |
| 53 | + end |
| 54 | + launch.DownloadPage = function() |
| 55 | + error("token exchange should not run for mismatched OAuth state") |
| 56 | + end |
| 57 | + |
| 58 | + local api = new("PoEAPI") |
| 59 | + local callbackArgs |
| 60 | + api:FetchAuthToken(function(response, errMsg, updateSettings) |
| 61 | + callbackArgs = { |
| 62 | + response = response, |
| 63 | + errMsg = errMsg, |
| 64 | + updateSettings = updateSettings, |
| 65 | + } |
| 66 | + end) |
| 67 | + |
| 68 | + assert.is_not_nil(launch.subScripts[123]) |
| 69 | + launch.subScripts[123].callback("auth-code", nil, "wrong-state", 12345) |
| 70 | + |
| 71 | + assert.is_nil(callbackArgs.response) |
| 72 | + assert.are.equals("OAuth state mismatch", callbackArgs.errMsg) |
| 73 | + assert.True(callbackArgs.updateSettings) |
| 74 | + assert.is_nil(api.authToken) |
| 75 | + end) |
| 76 | +end) |
0 commit comments