Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a188ab69cd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 161 lines (145 sloc) 7.992 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
;;; oauth-test.el --- Tests for oauth.el

;; Copyright (C) 2009 Peter Sanford

;; Author: Peter Sanford <peter AT petersdanceparty.com>

;; This file is NOT part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; This file contains tests for oauth.el. Currently the tests cover
;; the generation of HMAC-SHA1 signatures.
;;
;; The test data is taken from http://term.ie/oauth/example/

(require 'ert)
(require 'oauth)

(ert-deftest oauth-make-request ()
  (let ((req (oauth-make-request "Mercurochrome-knocked"
                                 "behave-retaking"
                                 (make-oauth-t
                                  :token "pearlier-Wallace\'s"
                                  :token-secret "Asunción-yuccas"))))
    (should (oauth-request-p req))
    (should (oauth-t-p (oauth-request-token req)))
    (let ((params (oauth-request-params req)))
      (loop for key in (list "oauth_consumer_key"
                             "oauth_timestamp"
                             "oauth_nonce"
                             "oauth_version") do
                             (should (assoc key params))))))

(ert-deftest oauth-request-to-header ()
  (let ((req (make-oauth-request
              :url "http://term.ie/oauth/example/request_token.php"
              :params '(("oauth_consumer_key" . "choleric-decolletes")
                        ("oauth_timestamp" . "1229298753")
                        ("oauth_nonce" . "0123456789")
                        ("oauth_signature_method" . "PLAINTEXT")
                        ("oauth_signature" . "dont_tell&")
                        ("oauth_version" . "1.0")
                        ("oauth_token" . "highballs-Croesus")))))
    (should (equal
     '(("Authorization" . "OAuth realm=\"\", oauth_consumer_key=\"choleric-decolletes\", oauth_nonce=\"0123456789\", oauth_signature=\"dont_tell%26\", oauth_signature_method=\"PLAINTEXT\", oauth_timestamp=\"1229298753\", oauth_token=\"highballs-Croesus\", oauth_version=\"1.0\""))
     (oauth-request-to-header req)))))

;; HMAC-SHA1 request
;; [oauth_version] => 1.0
;; [oauth_nonce] => 064f679764ac13d475e674672c106322
;; [oauth_timestamp] => 1231020688
;; [oauth_consumer_key] => key
;; [oauth_signature_method] => HMAC-SHA1
;; [http_method:private] => GET
;; [http_url:private] => http://term.ie/oauth/example/request_token.php
;; [base_string] => GET&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dkey%26oauth_nonce%3D064f679764ac13d475e674672c106322%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1231020688%26oauth_version%3D1.0
;; results in signature:
;; [oauth_signature] => grs+SdPE0DTE2+KYdlBXVk4Z+jc=
  
(ert-deftest hmac-sha1-base-string ()
  (let ((req (make-oauth-request
              :http-method "POST"
              :url "http://term.ie/oauth/example/request_token.php"
              :params '(("oauth_consumer_key" . "k&ey")
                        ("oauth_timestamp" . "1231020688")
                        ("oauth_nonce" . "064f679764ac13d475e674672c106322")
                        ("oauth_version" . "1.0")
                        ("oauth_signature_method" . "HMAC-SHA1")))))
    (should (equal
     "POST&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dk%2526ey%26oauth_nonce%3D064f679764ac13d475e674672c106322%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1231020688%26oauth_version%3D1.0"
     (oauth-build-signature-basestring-hmac-sha1 req)))))

(ert-deftest hmac-sha1-base-string-with-url-params ()
  (let ((req (make-oauth-request
              :http-method "POST"
              :url "http://term.ie/oauth/example/request_token.php?id=1234&other_param=true"
              :params '(("oauth_consumer_key" . "k&ey")
                        ("oauth_timestamp" . "1231020688")
                        ("oauth_nonce" . "064f679764ac13d475e674672c106322")
                        ("oauth_version" . "1.0")
                        ("oauth_signature_method" . "HMAC-SHA1")))))
    (should (equal
     "POST&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&id%3D1234%26oauth_consumer_key%3Dk%2526ey%26oauth_nonce%3D064f679764ac13d475e674672c106322%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1231020688%26oauth_version%3D1.0%26other_param%3Dtrue"
     (oauth-build-signature-basestring-hmac-sha1 req)))))

(ert-deftest hmac-sha1-request-signature ()
  (let ((req (make-oauth-request
              :url "http://term.ie/oauth/example/request_token.php"
              :params '(("oauth_consumer_key" . "key")
                        ("oauth_timestamp" . "1231020688")
                        ("oauth_nonce" . "064f679764ac13d475e674672c106322")
                        ("oauth_version" . "1.0")
                        ("oauth_signature_method" . "HMAC-SHA1"))))
        (secret "secret"))
    (should (equal
     "grs+SdPE0DTE2+KYdlBXVk4Z+jc="
     (oauth-build-signature-hmac-sha1 req secret)))))

(ert-deftest sign-request-hmac-sha1 ()
  (let ((req (make-oauth-request
              :url "http://term.ie/oauth/example/request_token.php"
              :params '(("oauth_consumer_key" . "key")
                        ("oauth_timestamp" . "1231020688")
                        ("oauth_nonce" . "064f679764ac13d475e674672c106322")
                        ("oauth_version" . "1.0"))))
        (secret "secret"))
    (oauth-sign-request-hmac-sha1 req secret)
    (should (equal
     "HMAC-SHA1"
     (cdr (assoc "oauth_signature_method" (oauth-request-params req)))))
    (should (equal
     "grs+SdPE0DTE2+KYdlBXVk4Z+jc="
     (cdr (assoc "oauth_signature" (oauth-request-params req)))))))


;; [parameters:private] => Array
;; (
;; [oauth_version] => 1.0
;; [oauth_nonce] => 25c105a828aaef0837213304376d95e6
;; [oauth_timestamp] => 1231121320
;; [oauth_consumer_key] => k&ey
;; [oauth_token] => requestkey
;; [oauth_signature_method] => HMAC-SHA1
;; [oauth_signature] => TatnsMj+1RF5LzRS1/i3IeQQ21g=
;; )
;; [http_method:private] => GET
;; [http_url:private] => http://term.ie/oauth/example/request_token.php
;; [base_string] => GET&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dk%2526ey%26oauth_nonce%3D25c105a828aaef0837213304376d95e6%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1231121320%26oauth_token%3Drequestkey%26oauth_version%3D1.0

(ert-deftest sign-request-with-token-hmac-sha1()
  (let ((req (make-oauth-request
               :url "http://term.ie/oauth/example/request_token.php"
               :params '(("oauth_consumer_key" . "k&ey")
                         ("oauth_version" . "1.0")
                         ("oauth_nonce" . "25c105a828aaef0837213304376d95e6")
                         ("oauth_timestamp" . "1231121320"))
               :token (make-oauth-t :token "requestkey"
                                    :token-secret "requestsecret")))
         (secret "secret"))
    (oauth-sign-request-hmac-sha1 req secret)
    (should (equal
     "HMAC-SHA1"
     (cdr (assoc "oauth_signature_method" (oauth-request-params req)))))
    (should (equal
     "TatnsMj+1RF5LzRS1/i3IeQQ21g="
     (cdr (assoc "oauth_signature" (oauth-request-params req)))))))
Something went wrong with that request. Please try again.