|
147 | 147 | minisky.auto_manage_tokens.should == false |
148 | 148 | minisky.default_progress.should == '*' |
149 | 149 | end |
| 150 | + |
| 151 | + describe '#token_expiration_date' do |
| 152 | + subject { Minisky.new(host, nil) } |
| 153 | + |
| 154 | + it 'should return nil for tokens with invalid encoding' do |
| 155 | + token = "bad\xC3".force_encoding('UTF-8') |
| 156 | + subject.token_expiration_date(token).should be_nil |
| 157 | + end |
| 158 | + |
| 159 | + it 'should return nil when the token does not have three parts' do |
| 160 | + subject.token_expiration_date('token').should be_nil |
| 161 | + subject.token_expiration_date('one.two').should be_nil |
| 162 | + subject.token_expiration_date('1.2.3.4').should be_nil |
| 163 | + |
| 164 | + token = make_token(Time.now + 3600) |
| 165 | + subject.token_expiration_date(token + '.qwe').should be_nil |
| 166 | + end |
| 167 | + |
| 168 | + it 'should return nil when the payload is not valid JSON' do |
| 169 | + token = ['header', Base64.strict_encode64('nope'), 'sig'].join('.') |
| 170 | + subject.token_expiration_date(token).should be_nil |
| 171 | + end |
| 172 | + |
| 173 | + it 'should return nil when exp field is missing' do |
| 174 | + token = ['header', Base64.strict_encode64(JSON.generate({ 'aud' => 'aaaa' })), 'sig'].join('.') |
| 175 | + subject.token_expiration_date(token).should be_nil |
| 176 | + end |
| 177 | + |
| 178 | + it 'should return nil when exp field is not a number' do |
| 179 | + token = ['header', Base64.strict_encode64(JSON.generate({ 'exp' => 'soon' })), 'sig'].join('.') |
| 180 | + subject.token_expiration_date(token).should be_nil |
| 181 | + end |
| 182 | + |
| 183 | + it 'should return nil when exp field is not a positive number' do |
| 184 | + token = ['header', Base64.strict_encode64(JSON.generate({ 'exp' => 0 })), 'sig'].join('.') |
| 185 | + subject.token_expiration_date(token).should be_nil |
| 186 | + end |
| 187 | + |
| 188 | + it 'should return nil when expiration year is before 2023' do |
| 189 | + token = make_token(Time.utc(2022, 12, 24, 19, 00, 00)) |
| 190 | + subject.token_expiration_date(token).should be_nil |
| 191 | + end |
| 192 | + |
| 193 | + it 'should return nil when expiration year is after 2100' do |
| 194 | + token = make_token(Time.utc(2101, 5, 5, 0, 0, 0)) |
| 195 | + subject.token_expiration_date(token).should be_nil |
| 196 | + end |
| 197 | + |
| 198 | + it 'should return the expiration time for a valid token' do |
| 199 | + time = Time.at(Time.now.to_i + 7200) |
| 200 | + token = make_token(time) |
| 201 | + subject.token_expiration_date(token).should == time |
| 202 | + end |
| 203 | + end |
| 204 | + |
| 205 | + describe '#access_token_expired?' do |
| 206 | + let(:config) { data } |
| 207 | + |
| 208 | + before do |
| 209 | + File.write('myconfig.yml', YAML.dump(config)) |
| 210 | + end |
| 211 | + |
| 212 | + context 'when there is no user config' do |
| 213 | + subject { Minisky.new(host, nil) } |
| 214 | + |
| 215 | + it 'should raise AuthError' do |
| 216 | + expect { subject.access_token_expired? }.to raise_error(Minisky::AuthError) |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + context 'when access token is missing' do |
| 221 | + let(:config) { data.merge('access_token' => nil) } |
| 222 | + |
| 223 | + it 'should raise AuthError' do |
| 224 | + expect { subject.access_token_expired? }.to raise_error(Minisky::AuthError) |
| 225 | + end |
| 226 | + end |
| 227 | + |
| 228 | + context 'when token expiration cannot be decoded' do |
| 229 | + let(:config) { data.merge('access_token' => 'blob') } |
| 230 | + |
| 231 | + it 'should raise AuthError' do |
| 232 | + expect { subject.access_token_expired? }.to raise_error(Minisky::AuthError) |
| 233 | + end |
| 234 | + end |
| 235 | + |
| 236 | + context 'when token expiration date is in the past' do |
| 237 | + let(:config) { data.merge('access_token' => make_token(Time.now - 30)) } |
| 238 | + |
| 239 | + it 'should return true' do |
| 240 | + subject.access_token_expired?.should == true |
| 241 | + end |
| 242 | + end |
| 243 | + |
| 244 | + context 'when token expiration date is in less than 60 seconds' do |
| 245 | + let(:config) { data.merge('access_token' => make_token(Time.now + 50)) } |
| 246 | + |
| 247 | + it 'should return true' do |
| 248 | + subject.access_token_expired?.should == true |
| 249 | + end |
| 250 | + end |
| 251 | + |
| 252 | + context 'when token expiration date is in more than 60 seconds' do |
| 253 | + let(:config) { data.merge('access_token' => make_token(Time.now + 180)) } |
| 254 | + |
| 255 | + it 'should return false' do |
| 256 | + subject.access_token_expired?.should == false |
| 257 | + end |
| 258 | + end |
| 259 | + end |
150 | 260 | end |
151 | 261 |
|
152 | 262 | describe 'in Minisky instance' do |
|
0 commit comments