summaryrefslogtreecommitdiff
path: root/http/codebird-php/test/signing_tests.php
blob: 882964ca7a6af6c2a11bc2a8a25558263312f9ab (plain)
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
162
163
164
165
166
<?php

namespace Codebird;
require_once ('test/codebirdm.php');

/**
 * A Twitter library in PHP.
 *
 * @package   codebird-test
 * @author    Jublo Solutions <support@jublo.net>
 * @copyright 2010-2016 Jublo Solutions <support@jublo.net>
 * @license   https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
 * @link      https://github.com/jublonet/codebird-php
 */

/**
 * OAuth signing tests
 *
 * @package codebird-test
 */
class Signing_Test extends \PHPUnit_Framework_TestCase
{
  /**
   * Initialise Codebird class
   *
   * @return \Codebird\Codebird The Codebird class
   */
  protected function getCB()
  {
    Codebird::setConsumerKey('123', '456');
    $cb = new CodebirdM();

    return $cb;
  }

  /**
   * Tests _url
   */
  public function testUrl()
  {
    $cb = $this->getCB();
    // string
    $this->assertEquals(
      'q%20%2B%20b%21%22%C2%A7%24%25%26%2F%28%29%3D%3F%23%2A13%2C3%C3%A4.',
      $cb->call('_url', ['q + b!"§$%&/()=?#*13,3ä.'])
    );
    // array
    $this->assertEquals(
      [
        'q%20%2B%20b%21%22%C2%A7%24%25%26%2F%28%29%3D%3F%23%2A13%2C3%C3%A4.',
        'test123'
      ],
      $cb->call('_url', [[
        'q + b!"§$%&/()=?#*13,3ä.',
        'test123'
      ]])
    );
  }

  /**
   * Tests _sha1
   */
  public function testSha1()
  {
    $cb = $this->getCB();
    $this->assertEquals(
      'ydAlpYjrGHU7psDQ9HPgHTcwEuw=',
      $cb->call('_sha1', ['q + b!"§$%&/()=?#*13,3ä.'])
    );

    // with access token secret
    $cb->setToken('678', '789');
    $this->assertEquals(
      'CtivZhAHiX49ZMUuHXtKabLAuo0=',
      $cb->call('_sha1', ['q + b!"§$%&/()=?#*13,3ä.'])
    );
  }

  /**
   * Tests _nonce
   */
  public function testNonce1()
  {
    $cb = $this->getCB();
    // default length
    $this->assertEquals(
      '4247c524',
      $cb->call('_nonce', [])
    );
    // custom length
    $this->assertEquals(
      '4247c5248da',
      $cb->call('_nonce', [11])
    );
  }

  /**
   * Tests _nonce
   * @expectedException \Exception
   * @expectedExceptionMessage Invalid nonce length.
   */
  public function testNonce2()
  {
    $cb = $this->getCB();
    // invalid length
    $cb->call('_nonce', [0]);
  }

  /**
   * Tests _getSignature
   */
  public function testGetSignature()
  {
    $cb = $this->getCB();
    $base_params = [
      'oauth_consumer_key' => '123',
      'oauth_nonce' => '12345678',
      'oauth_signature_method' => 'HMAC-SHA1',
      'oauth_timestamp' => '1400000000',
      'oauth_token' => '567',
      'oauth_version' => '1.0',
      'q' => 'Test search.'
    ];
    $this->assertEquals(
      'ZON/m9bHvciPdtyK9BlokjeiW4M=',
      $cb->call('_getSignature', ['GET', 'search/tweets', $base_params])
    );
  }

  /**
   * Tests _sign
   * @expectedException \Exception
   * @expectedExceptionMessage To generate a signature, the consumer key must be set.
   */
  public function testSign1()
  {
    $cb = $this->getCB();
    $cb->setConsumerKey(null, null);
    $params = ['q' => 'Test search.'];
    $cb->call('_sign', ['GET', 'search/tweets', $params]);
  }

  /**
   * Tests _sign
   */
  public function testSign2()
  {
    $cb = $this->getCB();
    $params = ['q' => 'Test search.'];
    $this->assertEquals(
      'OAuth oauth_consumer_key="123", oauth_nonce="4247c524", oauth_signature'
      . '="lOLNd5l6cGB9kWACxWLNKJwSD%2FI%3D", oauth_signature_method="HMAC-SHA'
      . '1", oauth_timestamp="1412345678", oauth_version="1.0"',
      $cb->call('_sign', ['GET', 'search/tweets', $params])
    );

    // with oauth token
    $cb->setToken('678', '789');
    $this->assertEquals(
      'OAuth oauth_consumer_key="123", oauth_nonce="4247c524", oauth_signature'
      . '="XzegzFKEqs2PpUMym5T%2BwhEmTz4%3D", oauth_signature_method="HMAC-SHA'
      . '1", oauth_timestamp="1412345678", oauth_token="678", oauth_version="1.0"',
      $cb->call('_sign', ['GET', 'search/tweets', $params])
    );
  }
}