File: auth_token_file_spec.rb

package info (click to toggle)
gist 6.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: ruby: 3,277; makefile: 16
file content (63 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download | duplicates (2)
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
describe Gist::AuthTokenFile do
  subject { Gist::AuthTokenFile }

  before(:each) do
    stub_const("Gist::URL_ENV_NAME", "STUBBED_GITHUB_URL")
  end

  describe "::filename" do
    let(:filename) { double() }

    context "with default GITHUB_URL" do
      it "is ~/.gist" do
        File.stub(:expand_path).and_call_original
        File.should_receive(:expand_path).with("~/.gist").and_return(filename)
        subject.filename.should be filename
      end
    end

    context "with custom GITHUB_URL" do
      before do
        ENV[Gist::URL_ENV_NAME] = github_url
      end
      let(:github_url) { "http://gh.custom.org:442/" }

      it "is ~/.gist.{custom_github_url}" do
        File.stub(:expand_path).and_call_original
        File.should_receive(:expand_path).with("~/.gist.http.gh.custom.org.442").and_return(filename)
        subject.filename.should be filename
      end
    end

  end

  describe "::read" do
    let(:token) { "auth_token" }

    it "reads file contents" do
      File.should_receive(:read).and_return(token)
      subject.read.should eq token
    end

    it "chomps file contents" do
      File.should_receive(:read).and_return(token + "\n")
      subject.read.should eq token
    end
  end

  describe "::write" do
    let(:token) { double() }
    let(:filename) { double() }
    let(:token_file) { double() }

    before do
      subject.stub(:filename) { filename }
    end

    it "writes token to file" do
      File.should_receive(:open).with(filename, 'w', 0600).and_yield(token_file)
      token_file.should_receive(:write).with(token)
      subject.write(token)
    end
  end
end