|
| 1 | +// |
| 2 | +// OAuth2_Tests.swift |
| 3 | +// OAuth2 Tests |
| 4 | +// |
| 5 | +// Created by Pascal Pfiffner on 6/6/14. |
| 6 | +// Copyright (c) 2014 Pascal Pfiffner. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +import OAuth2 |
| 11 | + |
| 12 | + |
| 13 | +class OAuth2_Tests: XCTestCase { |
| 14 | + |
| 15 | + func genericOAuth2() -> OAuth2 { |
| 16 | + return OAuth2(settings: [ |
| 17 | + "client_id": "abc", |
| 18 | + "api_uri": "https://api.ful.io", |
| 19 | + "authorize_uri": "https://auth.ful.io", |
| 20 | + "scope": "login", |
| 21 | + "verbose": true |
| 22 | + ]) |
| 23 | + } |
| 24 | + |
| 25 | + func testInit() { |
| 26 | + //var oauth = OAuth2(settings: NSDictionary()) // TODO: how to test that this raises? |
| 27 | + |
| 28 | + var oauth = OAuth2(settings: ["client_id": "def"]) |
| 29 | + XCTAssertFalse(oauth.verbose, "Non-verbose by default") |
| 30 | + XCTAssertEqualObjects(oauth.clientId, "def", "Must init `client_id`") |
| 31 | + |
| 32 | + let oa = self.genericOAuth2() |
| 33 | + XCTAssertEqualObjects(oa.apiURL, NSURL(string: "https://api.ful.io"), "Must init `api_uri`") |
| 34 | + XCTAssertEqualObjects(oa.authorizeURL, NSURL(string: "https://auth.ful.io"), "Must init `authorize_uri`") |
| 35 | + XCTAssertEqualObjects(oa.scope, "login", "Must init `scope`") |
| 36 | + XCTAssertTrue(oa.verbose, "Must init `verbose`") |
| 37 | + } |
| 38 | + |
| 39 | + func testAuthorizeURL() { |
| 40 | + let auth = self.genericOAuth2().authorizeURL("oauth2app://callback", scope: "launch", params: nil) |
| 41 | + |
| 42 | + let comp = NSURLComponents(URL: auth, resolvingAgainstBaseURL: true) |
| 43 | + XCTAssertEqualObjects("https", comp.scheme, "Need correct scheme") |
| 44 | + XCTAssertEqualObjects("auth.ful.io", comp.host, "Need correct host") |
| 45 | + |
| 46 | + let params = OAuth2.paramsFromQuery(comp.query) |
| 47 | + XCTAssertEqualObjects(params["redirect_uri"], "oauth2app://callback", "Expecting `` in query") |
| 48 | + XCTAssertEqualObjects(params["scope"], "launch", "Expecting `scope` in query") |
| 49 | +// XCTAssertTrue(String(params["state"] as String).utf16count > 0, "Expecting `state` in query") |
| 50 | + } |
| 51 | + |
| 52 | + func testQueryParamConversion() { |
| 53 | + let qry = OAuth2.queryStringFor(["a": "AA", "b": "BB", "x": "yz"]) |
| 54 | + XCTAssertTrue(14 == qry.utf16count, "Expecting a 14 character string") |
| 55 | + |
| 56 | + let dict = OAuth2.paramsFromQuery(qry) |
| 57 | + XCTAssertEqualObjects(dict["a"], "AA", "Must unpack `a`") |
| 58 | + XCTAssertEqualObjects(dict["b"], "BB", "Must unpack `b`") |
| 59 | + XCTAssertEqualObjects(dict["x"], "yz", "Must unpack `x`") |
| 60 | + } |
| 61 | +} |
0 commit comments