Sha256: fbbe8804e14ae668de7fd2d6dfd3761f96d19d02e53dbeb1d1b1e9a2793eb133

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'spec_helper'

describe Tokenie do
  describe ".friendly" do
    it "generates a token with 6 characters by default" do
      SecureRandom.stub(:base64) { 'qwertytoken' }
      Tokenie.friendly.should eq('qwerty')
    end

    it "generates a token with 2 characters as passed" do
      SecureRandom.stub(:base64) { 'qwerty' }
      Tokenie.friendly(:length => 2).should eq('qw')
    end

    it "generates a friendly token" do
      SecureRandom.stub(:base64) { '+/=lIO0o' }
      Tokenie.friendly(:length => 8).should eq('abcdefgh')
    end

    it "generates an unique token" do
      existing_tokens = ['qwerty', 'abcdef']
      generation_line = ['qwerty', 'abcdef', 'abcde1', 'abcde2']
      SecureRandom.stub(:base64) { generation_line.shift }

      token = Tokenie.friendly { |t| existing_tokens.include?(t) }
      token.should eq('abcde1')
    end

    it "raises an argument error if length is greater than 16" do
      expect {
        Tokenie.friendly(:length => 17)
      }.to raise_error("'length' must be in range (2..16)")
    end

    it "raises an argument error if length is less than 2" do
      expect {
        Tokenie.friendly(:length => 1)
      }.to raise_error("'length' must be in range (2..16)")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tokenie-0.0.1 spec/tokenie_spec.rb