-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for upload and delete Avatar API
- Loading branch information
1 parent
bc6ea18
commit 7b31944
Showing
13 changed files
with
594 additions
and
34 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// | ||
// UsersModuleIntegrationSpecs.swift | ||
// BoxSDKIntegrationTests-iOS | ||
// | ||
// Created by Artur Jankowski on 11/07/2022. | ||
// Copyright © 2022 box. All rights reserved. | ||
// | ||
|
||
@testable import BoxSDK | ||
import Nimble | ||
import Quick | ||
|
||
class UsersModuleIntegrationSpecs: BaseIntegrationSpecs { | ||
override func spec() { | ||
beforeSuite { | ||
self.initializeClient() | ||
} | ||
|
||
describe("Users Module") { | ||
context("avatar") { | ||
var user: User? | ||
|
||
beforeEach { | ||
self.createUser(name: NameGenerator.getUniqueName(for: "user")) { createdUser in | ||
user = createdUser | ||
} | ||
} | ||
|
||
afterEach { | ||
self.deleteUser(user) | ||
user = nil | ||
} | ||
|
||
it("should correctly add get delete using Data") { | ||
guard let user = user else { | ||
fail("An error occurred during setup initial data") | ||
return | ||
} | ||
|
||
// Add | ||
let name = IntegrationTestResources.smallImage.fileName | ||
let data = FileUtil.getFileContent(fileName: name)! | ||
|
||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.uploadAvatar(userId: user.id, data: data, name: name) { result in | ||
switch result { | ||
case let .success(uploadItem): | ||
expect(uploadItem.picUrls.preview).toNot(beNil() && beEmpty()) | ||
expect(uploadItem.picUrls.small).toNot(beNil() && beEmpty()) | ||
expect(uploadItem.picUrls.large).toNot(beNil() && beEmpty()) | ||
case let .failure(error): | ||
fail("Expected uploadAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
|
||
// Get | ||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.getAvatar(userId: user.id) { result in | ||
switch result { | ||
case let .success(data): | ||
expect(data).toNot(beNil()) | ||
case let .failure(error): | ||
fail("Expected getAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
|
||
// Delete | ||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.deleteAvatar(userId: user.id) { result in | ||
switch result { | ||
case .success: | ||
break | ||
case let .failure(error): | ||
fail("Expected deleteAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
} | ||
|
||
it("should correctly add get delete using InputStream") { | ||
guard let user = user else { | ||
fail("An error occurred during setup initial data") | ||
return | ||
} | ||
|
||
// Add | ||
let name = IntegrationTestResources.smallImage.fileName | ||
let path = FileUtil.getFilePath(fileName: name) | ||
let inputStream = InputStream(fileAtPath: path)! | ||
|
||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.streamUploadAvatar(userId: user.id, stream: inputStream, name: name) { result in | ||
switch result { | ||
case let .success(uploadItem): | ||
expect(uploadItem.picUrls.preview).toNot(beNil() && beEmpty()) | ||
expect(uploadItem.picUrls.small).toNot(beNil() && beEmpty()) | ||
expect(uploadItem.picUrls.large).toNot(beNil() && beEmpty()) | ||
case let .failure(error): | ||
fail("Expected streamUploadAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
|
||
// Get | ||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.getAvatar(userId: user.id) { result in | ||
switch result { | ||
case let .success(data): | ||
expect(data).toNot(beNil()) | ||
case let .failure(error): | ||
fail("Expected getAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
|
||
// Delete | ||
waitUntil(timeout: .seconds(Constants.Timeout.large)) { done in | ||
self.client.users.deleteAvatar(userId: user.id) { result in | ||
switch result { | ||
case .success: | ||
break | ||
case let .failure(error): | ||
fail("Expected deleteAvatar call to succeed, but instead got \(error)") | ||
} | ||
|
||
done() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// MimeTypeProvider.swift | ||
// BoxSDK-iOS | ||
// | ||
// Created by Artur Jankowski on 08/07/2022. | ||
// Copyright © 2022 box. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
#if os(iOS) | ||
#if canImport(UniformTypeIdentifiers) | ||
import UniformTypeIdentifiers | ||
#endif | ||
|
||
#if canImport(MobileCoreServices) | ||
import MobileCoreServices | ||
#endif | ||
#endif | ||
|
||
/// Provides method for converting given filename to mime type | ||
enum MimeTypeProvider { | ||
|
||
/// Converting given filename to mime type | ||
/// | ||
/// - Parameters: | ||
/// - filename: A file name with extension (e.g. image.png) | ||
/// - Returns: A mime type | ||
static func getMimeTypeFrom(filename: String) -> String { | ||
let defaultMimeType = "application/octet-stream" | ||
let url = NSURL(fileURLWithPath: filename) | ||
|
||
guard let pathExtension = url.pathExtension else { | ||
return defaultMimeType | ||
} | ||
|
||
if #available(iOS 14, *) { | ||
return UTType(filenameExtension: pathExtension)?.preferredMIMEType ?? defaultMimeType | ||
} | ||
else { | ||
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue(), | ||
let mimeType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() { | ||
return mimeType as String | ||
} | ||
} | ||
|
||
return defaultMimeType | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.