//
// C o p y r i g h t ( c ) 2 0 1 9 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
//
import Foundation
import SignalServiceKit
import XCTest
class OWSLinkPreviewTest : SSKBaseTestSwift {
override func setUp ( ) {
super . setUp ( )
// P u t s e t u p c o d e h e r e . T h i s m e t h o d i s c a l l e d b e f o r e t h e i n v o c a t i o n o f e a c h t e s t m e t h o d i n t h e c l a s s .
}
override func tearDown ( ) {
// P u t t e a r d o w n c o d e h e r e . T h i s m e t h o d i s c a l l e d a f t e r t h e i n v o c a t i o n o f e a c h t e s t m e t h o d i n t h e c l a s s .
super . tearDown ( )
}
func testBuildValidatedLinkPreview_TitleAndImage ( ) {
let url = " https://www.youtube.com/watch?v=tP-Ipsat90c "
let body = " \( url ) "
let previewBuilder = SSKProtoDataMessagePreview . builder ( url : url )
previewBuilder . setTitle ( " Some Youtube Video " )
let imageAttachmentBuilder = SSKProtoAttachmentPointer . builder ( id : 1 )
imageAttachmentBuilder . setKey ( Randomness . generateRandomBytes ( 32 ) )
imageAttachmentBuilder . setContentType ( OWSMimeTypeImageJpeg )
previewBuilder . setImage ( try ! imageAttachmentBuilder . build ( ) )
let dataBuilder = SSKProtoDataMessage . builder ( )
dataBuilder . addPreview ( try ! previewBuilder . build ( ) )
self . readWrite { ( transaction ) in
XCTAssertNotNil ( try ! OWSLinkPreview . buildValidatedLinkPreview ( dataMessage : try ! dataBuilder . build ( ) ,
body : body ,
transaction : transaction ) )
}
}
func testBuildValidatedLinkPreview_Title ( ) {
let url = " https://www.youtube.com/watch?v=tP-Ipsat90c "
let body = " \( url ) "
let previewBuilder = SSKProtoDataMessagePreview . builder ( url : url )
previewBuilder . setTitle ( " Some Youtube Video " )
let dataBuilder = SSKProtoDataMessage . builder ( )
dataBuilder . addPreview ( try ! previewBuilder . build ( ) )
self . readWrite { ( transaction ) in
XCTAssertNotNil ( try ! OWSLinkPreview . buildValidatedLinkPreview ( dataMessage : try ! dataBuilder . build ( ) ,
body : body ,
transaction : transaction ) )
}
}
func testBuildValidatedLinkPreview_Image ( ) {
let url = " https://www.youtube.com/watch?v=tP-Ipsat90c "
let body = " \( url ) "
let previewBuilder = SSKProtoDataMessagePreview . builder ( url : url )
let imageAttachmentBuilder = SSKProtoAttachmentPointer . builder ( id : 1 )
imageAttachmentBuilder . setKey ( Randomness . generateRandomBytes ( 32 ) )
imageAttachmentBuilder . setContentType ( OWSMimeTypeImageJpeg )
previewBuilder . setImage ( try ! imageAttachmentBuilder . build ( ) )
let dataBuilder = SSKProtoDataMessage . builder ( )
dataBuilder . addPreview ( try ! previewBuilder . build ( ) )
self . readWrite { ( transaction ) in
XCTAssertNotNil ( try ! OWSLinkPreview . buildValidatedLinkPreview ( dataMessage : try ! dataBuilder . build ( ) ,
body : body ,
transaction : transaction ) )
}
}
func testBuildValidatedLinkPreview_NoTitleOrImage ( ) {
let url = " https://www.youtube.com/watch?v=tP-Ipsat90c "
let body = " \( url ) "
let previewBuilder = SSKProtoDataMessagePreview . builder ( url : url )
let dataBuilder = SSKProtoDataMessage . builder ( )
dataBuilder . addPreview ( try ! previewBuilder . build ( ) )
self . readWrite { ( transaction ) in
do {
_ = try OWSLinkPreview . buildValidatedLinkPreview ( dataMessage : try ! dataBuilder . build ( ) ,
body : body ,
transaction : transaction )
XCTFail ( " Missing expected error. " )
} catch {
// D o n o t h i n g .
}
}
}
func testIsValidLinkUrl ( ) {
XCTAssertTrue ( OWSLinkPreview . isValidLinkUrl ( " https://www.youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertTrue ( OWSLinkPreview . isValidLinkUrl ( " https://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertTrue ( OWSLinkPreview . isValidLinkUrl ( " https://www.youtube.com " ) )
// A l l o w a r b i t r a r y s u b d o m a i n s .
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://some.random.subdomain.youtube.com/watch?v=tP-Ipsat90c " ) )
// D o n ' t a l l o w H T T P , o n l y H T T P S
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " http://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " mailto://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " ftp://youtube.com/watch?v=tP-Ipsat90c " ) )
// D o n ' t a l l o w s i m i l a r d o m a i n s .
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://xyoutube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://youtubex.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://youtube.comx/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://www.xyoutube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://www.youtubex.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://www.youtube.comx/watch?v=tP-Ipsat90c " ) )
// D o n ' t a l l o w m e d i a d o m a i n s .
XCTAssertFalse ( OWSLinkPreview . isValidLinkUrl ( " https://i.ytimg.com/vi/tP-Ipsat90c/maxresdefault.jpg " ) )
}
func testIsValidMediaUrl ( ) {
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://www.youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://www.youtube.com " ) )
// A l l o w a r b i t r a r y s u b d o m a i n s .
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://some.random.subdomain.youtube.com/watch?v=tP-Ipsat90c " ) )
// D o n ' t a l l o w H T T P , o n l y H T T P S
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " http://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " mailto://youtube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " ftp://youtube.com/watch?v=tP-Ipsat90c " ) )
// D o n ' t a l l o w s i m i l a r d o m a i n s .
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://xyoutube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://youtubex.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://youtube.comx/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://www.xyoutube.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://www.youtubex.com/watch?v=tP-Ipsat90c " ) )
XCTAssertFalse ( OWSLinkPreview . isValidMediaUrl ( " https://www.youtube.comx/watch?v=tP-Ipsat90c " ) )
// A l l o w m e d i a d o m a i n s .
XCTAssertTrue ( OWSLinkPreview . isValidMediaUrl ( " https://i.ytimg.com/vi/tP-Ipsat90c/maxresdefault.jpg " ) )
}
func testPreviewUrlForMessageBodyText ( ) {
XCTAssertNil ( OWSLinkPreview . previewUrl ( forMessageBodyText : " " ) )
XCTAssertNil ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim " ) )
XCTAssertNil ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim http:// " ) )
XCTAssertNil ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim http://a.com " ) )
XCTAssertEqual ( OWSLinkPreview . previewUrl ( forMessageBodyText : " https://www.youtube.com/watch?v=tP-Ipsat90c " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
XCTAssertEqual ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
// I f t h e r e a r e m o r e t h a n o n e , t a k e t h e f i r s t .
XCTAssertEqual ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim https://www.youtube.com/watch?v=other-url carol " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
}
}