#lang racket
(require "dropbox.rkt"
rackunit
json
file/sha1
net/url)
(check-equal?
(get-authorization-url #:callback "http://yahoo.com")
(string-append "https://www.dropbox.com/1/oauth/authorize"
"?"
"oauth_token="
OAUTH-REQUEST-TOKEN
"&"
"oauth_callback=http://yahoo.com&locale=en"))
(define account-info (get-account-info))
(check-equal? (hash-ref account-info 'display_name)
"Stephen Chang")
(check-equal? (hash-ref account-info 'uid)
132816807)
(define PDF-FILE "dropbox.pdf")
(define PNG-FILE "dropbox.png")
(define BIG-FILE "xmas.jpg")
(define TEST-DIR "test-files")
(define PDF-PATH (string-append TEST-DIR "/" PDF-FILE))
(define PNG-PATH (string-append TEST-DIR "/" PNG-FILE))
(define BIG-PATH (string-append TEST-DIR "/" BIG-FILE))
(define (equa? . args)
(define x (first args))
(for/and ([y (rest args)]) (equal? x y)))
(define-syntax (check-field: stx)
(syntax-case stx (in-metas: vals:)
[(_ fld in-metas: m ... equal-to: v)
(syntax/loc stx (check-true (equa? (hash-ref m (quote fld)) ... v)))]))
(define-syntax (check-field-equal: stx)
(syntax-case stx (in-metas:)
[(_ fld in-metas: m ...)
(syntax/loc stx (check-true (equa? (hash-ref m (quote fld)) ...)))]))
(define-syntax (check-field-not-equal: stx)
(syntax-case stx (in-metas:)
[(_ fld in-metas: m1 m2)
(syntax/loc stx (check-false (equal? (hash-ref m1 (quote fld))
(hash-ref m2 (quote fld)))))]))
(define (filter-search-by-exact-match filename search-res)
(filter (λ (m) (string=? (hash-ref m 'path)
(string-append "/" filename)))
search-res))
(define (do-upload/download-test localfile remote-file
#:remote-dir [remote-dir ""]
#:no-sha1? [no-sha1? #f])
(define remotefullpath (if (string=? remote-dir "")
remote-file
(string-append remote-dir "/" remote-file)))
(define orig-size (file-size localfile))
(define orig-sha1 (if no-sha1? "" (call-with-input-file localfile sha1)))
(unless (string=? remote-dir "")
(if (null? (search "" remote-dir))
(create-folder remote-dir)
(begin
(delete remote-dir)
(create-folder remote-dir))))
(define uploaded-meta
(upload-file localfile remotefullpath #:overwrite? "true"))
(check-true (jsexpr? uploaded-meta)) (define uploaded-meta-from-revlst (first (get-revisions remotefullpath)))
(define up-rev (hash-ref uploaded-meta 'rev))
(define meta (get-metadata remotefullpath))
(check-field-equal: rev
in-metas: uploaded-meta uploaded-meta-from-revlst meta)
(check-field: bytes
in-metas: uploaded-meta uploaded-meta-from-revlst meta
equal-to: orig-size)
(define search-meta
(filter-search-by-exact-match remotefullpath (search remote-dir remote-file)))
(check-false (null? search-meta))
(check-field-equal: rev
in-metas: (first search-meta) uploaded-meta)
(define copied-file (string-append "COPIED" remote-file))
(define copiedfullpath
(if (string=? remote-dir "")
copied-file
(string-append remote-dir "/" copied-file)))
(when (exists? remote-dir copied-file)
(delete copiedfullpath))
(define copied-meta (copy remotefullpath copiedfullpath))
(define copied-search-meta
(filter-search-by-exact-match copiedfullpath (search remote-dir copied-file)))
(check-false (null? copied-search-meta))
(define copyref (hash-ref (get-copy-ref remotefullpath) 'copy_ref))
(define copyref-copied-file (string-append "COPYREF" remote-file))
(define copyref-copiedfullpath
(if (string=? remote-dir "")
copyref-copied-file
(string-append remote-dir "/" copyref-copied-file)))
(when (exists? remote-dir copyref-copied-file)
(delete copyref-copiedfullpath))
(define copyref-copied-meta (copy "" copyref-copiedfullpath #:copy-ref copyref))
(define copyref-copied-search-meta
(filter-search-by-exact-match copyref-copiedfullpath
(search remote-dir copyref-copied-file)))
(check-false (null? copyref-copied-search-meta))
(check-field: bytes
in-metas: copied-meta (first copied-search-meta)
copyref-copied-meta (first copyref-copied-search-meta)
equal-to: orig-size)
(define moved-file (string-append "MOVED" remote-file))
(define movedfullpath
(if (string=? remote-dir "")
moved-file
(string-append remote-dir "/" moved-file)))
(when (exists? remote-dir moved-file)
(delete movedfullpath))
(define moved-meta (move copiedfullpath movedfullpath))
(define moved-search-meta
(filter-search-by-exact-match movedfullpath (search remote-dir moved-file)))
(check-false (null? moved-search-meta))
(check-field: bytes
in-metas: moved-meta (first moved-search-meta)
equal-to: orig-size)
(delete copyref-copiedfullpath)
(delete movedfullpath)
(define share-url-short
(string->url (hash-ref (get-share-url remotefullpath) 'url)))
(define share-url
(string->url (hash-ref (get-share-url remotefullpath #:short-url "false") 'url)))
(define media-url
(string->url (hash-ref (get-media-url remotefullpath) 'url)))
(check-equal? (url-scheme share-url-short) "http")
(check-equal? (url-scheme share-url) "https")
(check-equal? (url-scheme media-url) "https")
(check-equal? (url-host share-url-short) "db.tt")
(check-equal? (url-host share-url) "www.dropbox.com")
(check-equal? (url-host media-url) "dl.dropbox.com")
(check-equal? (last (map path/param-path (url-path share-url)))
remote-file)
(check-equal? (last (map path/param-path (url-path media-url)))
remote-file)
(check-true (file-exists? localfile))
(delete-file localfile)
(check-false (file-exists? localfile))
(download-file remotefullpath localfile #:exists 'replace)
(define new-size (file-size localfile))
(define new-sha1 (if no-sha1? "" (call-with-input-file localfile sha1)))
(check-equal? orig-size new-size)
(check-equal? orig-sha1 new-sha1)
(define deleted-meta (delete remotefullpath))
(define deleted-meta-from-revlst (first (get-revisions remotefullpath)))
(check-false (exists? remote-dir remote-file))
(check-field-equal: rev
in-metas: deleted-meta deleted-meta-from-revlst)
(check-field: bytes
in-metas: deleted-meta deleted-meta-from-revlst
equal-to: 0)
(check-field-not-equal: rev
in-metas: meta deleted-meta)
(define restored-meta (restore-file remotefullpath up-rev))
(define restored-meta-from-revlst (first (get-revisions remotefullpath)))
(check-true (exists? remote-dir remote-file))
(check-field-not-equal: rev
in-metas: restored-meta deleted-meta)
(check-field-not-equal: rev
in-metas: restored-meta uploaded-meta)
(check-field-equal: rev
in-metas: restored-meta restored-meta-from-revlst)
(check-field: bytes
in-metas: restored-meta restored-meta-from-revlst
equal-to: orig-size)
(check-true (file-exists? localfile))
(delete-file localfile)
(check-false (file-exists? localfile))
(download-file remotefullpath localfile #:exists 'replace)
(define new-size2 (file-size localfile))
(define new-sha12 (if no-sha1? "" (call-with-input-file localfile sha1)))
(check-equal? orig-size new-size2)
(check-equal? orig-sha1 new-sha12)
(delete remotefullpath)
(check-field: path
in-metas: uploaded-meta uploaded-meta-from-revlst meta
deleted-meta deleted-meta-from-revlst
restored-meta restored-meta-from-revlst
equal-to: (string-append "/" remotefullpath))
)
(do-upload/download-test PDF-PATH PDF-FILE)
(do-upload/download-test PNG-PATH PNG-FILE)
(do-upload/download-test BIG-PATH BIG-FILE)
(do-upload/download-test PDF-PATH PDF-FILE #:remote-dir TEST-DIR)
(do-upload/download-test PNG-PATH PNG-FILE #:remote-dir TEST-DIR)
(do-upload/download-test BIG-PATH BIG-FILE #:remote-dir TEST-DIR)
(get-delta)
(get-image-thumbnail "xmas.jpg" "xmas-thumb.jpg"
#:size "s" #:exists 'replace)
(upload-large-file "test-files/xmas.jpg" "xmas.jpg"
#:verbose? #t
)