From 380ef3f77a2f58360128ac62bf3ce7635873ca47 Mon Sep 17 00:00:00 2001 From: Piet Brauer Date: Wed, 4 Nov 2015 10:07:00 +0700 Subject: [PATCH] Add test for getting me and my emails --- TrashCanKit.xcodeproj/project.pbxproj | 8 +++++ TrashCanKitTests/Fixtures/Emails.json | 18 ++++++++++ TrashCanKitTests/Me.json | 29 ++++++++++++++++ TrashCanKitTests/UserTests.swift | 50 +++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 TrashCanKitTests/Fixtures/Emails.json create mode 100644 TrashCanKitTests/Me.json diff --git a/TrashCanKit.xcodeproj/project.pbxproj b/TrashCanKit.xcodeproj/project.pbxproj index f9a08a4..9db6cf9 100644 --- a/TrashCanKit.xcodeproj/project.pbxproj +++ b/TrashCanKit.xcodeproj/project.pbxproj @@ -16,6 +16,8 @@ 231B685A1BE9A3DD007AE2A6 /* TokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 231B68591BE9A3DD007AE2A6 /* TokenTests.swift */; }; 231B685C1BE9A699007AE2A6 /* refresh_token_error.json in Resources */ = {isa = PBXBuildFile; fileRef = 231B685B1BE9A699007AE2A6 /* refresh_token_error.json */; }; 231B685F1BE9AAC6007AE2A6 /* Repositories.json in Resources */ = {isa = PBXBuildFile; fileRef = 231B685D1BE9AA66007AE2A6 /* Repositories.json */; }; + 231B68611BE9AD30007AE2A6 /* Me.json in Resources */ = {isa = PBXBuildFile; fileRef = 231B68601BE9AD30007AE2A6 /* Me.json */; }; + 231B68631BE9AD8D007AE2A6 /* Emails.json in Resources */ = {isa = PBXBuildFile; fileRef = 231B68621BE9AD8D007AE2A6 /* Emails.json */; }; 236364741BE22F1000AA7104 /* TrashCanKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 236364731BE22F1000AA7104 /* TrashCanKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; 2363647B1BE22F1000AA7104 /* TrashCanKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 236364701BE22F1000AA7104 /* TrashCanKit.framework */; }; 2363648E1BE22FCC00AA7104 /* TrashCanKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2363648A1BE22FCC00AA7104 /* TrashCanKit.swift */; }; @@ -69,6 +71,8 @@ 231B68591BE9A3DD007AE2A6 /* TokenTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokenTests.swift; sourceTree = ""; }; 231B685B1BE9A699007AE2A6 /* refresh_token_error.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = refresh_token_error.json; sourceTree = ""; }; 231B685D1BE9AA66007AE2A6 /* Repositories.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = Repositories.json; path = Fixtures/Repositories.json; sourceTree = ""; }; + 231B68601BE9AD30007AE2A6 /* Me.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Me.json; sourceTree = ""; }; + 231B68621BE9AD8D007AE2A6 /* Emails.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = Emails.json; path = Fixtures/Emails.json; sourceTree = ""; }; 236364701BE22F1000AA7104 /* TrashCanKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TrashCanKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 236364731BE22F1000AA7104 /* TrashCanKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TrashCanKit.h; sourceTree = ""; }; 236364751BE22F1000AA7104 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -168,6 +172,8 @@ 231B68571BE9A13C007AE2A6 /* authorize.json */, 231B685B1BE9A699007AE2A6 /* refresh_token_error.json */, 231B685D1BE9AA66007AE2A6 /* Repositories.json */, + 231B68601BE9AD30007AE2A6 /* Me.json */, + 231B68621BE9AD8D007AE2A6 /* Emails.json */, ); name = Fixtures; sourceTree = ""; @@ -280,11 +286,13 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 231B68611BE9AD30007AE2A6 /* Me.json in Resources */, 231B685F1BE9AAC6007AE2A6 /* Repositories.json in Resources */, 231B68581BE9A13C007AE2A6 /* authorize.json in Resources */, 231B685C1BE9A699007AE2A6 /* refresh_token_error.json in Resources */, 236364A51BE2313B00AA7104 /* Email.json in Resources */, 236364A71BE2313B00AA7104 /* User.json in Resources */, + 231B68631BE9AD8D007AE2A6 /* Emails.json in Resources */, 236364A61BE2313B00AA7104 /* Repository.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/TrashCanKitTests/Fixtures/Emails.json b/TrashCanKitTests/Fixtures/Emails.json new file mode 100644 index 0000000..e4957d0 --- /dev/null +++ b/TrashCanKitTests/Fixtures/Emails.json @@ -0,0 +1,18 @@ +{ + "page": 1, + "pagelen": 10, + "size": 1, + "values": [ + { + "email": "tutorials@bitbucket.org", + "is_confirmed": true, + "is_primary": true, + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/user/emails/tutorials@bitbucket.org" + } + }, + "type": "email" + } + ] +} \ No newline at end of file diff --git a/TrashCanKitTests/Me.json b/TrashCanKitTests/Me.json new file mode 100644 index 0000000..b13089c --- /dev/null +++ b/TrashCanKitTests/Me.json @@ -0,0 +1,29 @@ +{ + "created_on": "2011-12-20T16:34:07.132459+00:00", + "display_name": "tutorials account", + "links": { + "avatar": { + "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png" + }, + "followers": { + "href": "https://api.bitbucket.org/2.0/users/tutorials/followers" + }, + "following": { + "href": "https://api.bitbucket.org/2.0/users/tutorials/following" + }, + "html": { + "href": "https://bitbucket.org/tutorials" + }, + "repositories": { + "href": "https://api.bitbucket.org/2.0/repositories/tutorials" + }, + "self": { + "href": "https://api.bitbucket.org/2.0/users/tutorials" + } + }, + "location": "Santa Monica, CA", + "type": "user", + "username": "tutorials", + "uuid": "{c788b2da-b7a2-404c-9e26-d3f077557007}", + "website": "https://tutorials.bitbucket.org/" +} \ No newline at end of file diff --git a/TrashCanKitTests/UserTests.swift b/TrashCanKitTests/UserTests.swift index 9ffa26f..2b1c662 100644 --- a/TrashCanKitTests/UserTests.swift +++ b/TrashCanKitTests/UserTests.swift @@ -1,7 +1,19 @@ import XCTest +import Nocilla import TrashCanKit class UserTests: XCTestCase { + override func setUp() { + super.setUp() + LSNocilla.sharedInstance().start() + } + + override func tearDown() { + super.tearDown() + LSNocilla.sharedInstance().clearStubs() + LSNocilla.sharedInstance().stop() + } + func testConstructFromJSON() { let subject = User(TestHelper.loadJSON("User")) XCTAssertEqual(subject.id, "{e9f0168c-cdf8-404a-95bb-3943dd2a65b6}") @@ -16,4 +28,42 @@ class UserTests: XCTestCase { XCTAssertEqual(subject.email, "me@supercooldomain.io") XCTAssertEqual(subject.type, "email") } + + func testMe() { + let tokenConfig = TokenConfiguration("123456", refreshToken: "7890") + stubRequest("GET", "https://bitbucket.org/api/2.0/user?access_token=123456").andReturn(200).withBody(TestHelper.loadJSONString("Me")) + let expectation = expectationWithDescription("get_me") + TrashCanKit(tokenConfig).me() { response in + switch response { + case .Success(let user): + XCTAssertEqual(user.name, "tutorials account") + expectation.fulfill() + case .Failure: + XCTAssertFalse(true) + expectation.fulfill() + } + } + waitForExpectationsWithTimeout(1) { error in + XCTAssertNil(error) + } + } + + func testMyEmail() { + let tokenConfig = TokenConfiguration("123456", refreshToken: "7890") + stubRequest("GET", "https://bitbucket.org/api/2.0/user/emails?access_token=123456").andReturn(200).withBody(TestHelper.loadJSONString("Emails")) + let expectation = expectationWithDescription("get_me") + TrashCanKit(tokenConfig).emails() { response in + switch response { + case .Success(let emails): + XCTAssertEqual(emails.first?.email, "tutorials@bitbucket.org") + expectation.fulfill() + case .Failure: + XCTAssertFalse(true) + expectation.fulfill() + } + } + waitForExpectationsWithTimeout(1) { error in + XCTAssertNil(error) + } + } }