aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/admin_public_body_controller_spec.rb
blob: 6b88fe39d86c530dac03a2aa86f6e52b587ceb2f (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AdminPublicBodyController, "when administering public bodies" do
    integrate_views
    fixtures :public_bodies, :public_body_translations

    before do
        username = MySociety::Config.get('ADMIN_USERNAME', '')
        password = MySociety::Config.get('ADMIN_PASSWORD', '')
        basic_auth_login @request
    end


    it "shows the index page" do
        get :index
    end

    it "searches for 'humpa'" do
        get :index, :query => "humpa"
        assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ]
    end

    it "shows a public body" do
        get :show, :id => 2
    end

    it "creates a new public body" do
        PublicBody.count.should == 2
        post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } }
        PublicBody.count.should == 3
    end

    it "edits a public body" do
        get :edit, :id => 2
    end

    it "saves edits to a public body" do
        public_bodies(:humpadink_public_body).name.should == "Department for Humpadinking"
        post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' } }
        response.flash[:notice].should include('successful')
        pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
        pb.name.should == "Renamed"
    end

    it "destroys a public body" do
        PublicBody.count.should == 2
        post :destroy, { :id => 3 }
        PublicBody.count.should == 1
    end

    it "sets a using_admin flag" do
        get :show, :id => 2
        session[:using_admin].should == 1
    end

    it "disallows non-authenticated users to do anything" do
        @request.env["HTTP_AUTHORIZATION"] = ""
        PublicBody.count.should == 2
        post :destroy, { :id => 3 }
        response.code.should == "401"
        PublicBody.count.should == 2
        session[:using_admin].should == nil
    end

    it "skips admin authorisation when no username/password set" do
        config = MySociety::Config.load_default()
        config['ADMIN_USERNAME'] = ''
        config['ADMIN_PASSWORD'] = ''
        @request.env["HTTP_AUTHORIZATION"] = ""
        PublicBody.count.should == 2
        post :destroy, { :id => 3 }
        PublicBody.count.should == 1
        session[:using_admin].should == 1
    end
    it "skips admin authorisation when no username set" do
        config = MySociety::Config.load_default()
        config['ADMIN_USERNAME'] = ''
        config['ADMIN_PASSWORD'] = 'fuz'
        @request.env["HTTP_AUTHORIZATION"] = ""
        PublicBody.count.should == 2
        post :destroy, { :id => 3 }
        PublicBody.count.should == 1
        session[:using_admin].should == 1
    end


end

describe AdminPublicBodyController, "when administering public bodies with i18n" do
    integrate_views
    fixtures :public_bodies, :public_body_translations
  
    before do
        username = MySociety::Config.get('ADMIN_USERNAME', '')
        password = MySociety::Config.get('ADMIN_PASSWORD', '')
        basic_auth_login @request
    end

    it "shows the index page" do
        get :index
    end

    it "searches for 'humpa'" do
        get :index, {:query => "humpa", :locale => "es"}
        assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ]
    end

    it "shows a public body" do
        get :show, {:id => 2, :locale => "es" }
    end

    it "creates a new public body" do
        I18n.locale = :es
        PublicBody.count.should == 2
        post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } }
        PublicBody.count.should == 3
        I18n.locale = :en
    end

    it "edits a public body" do
        I18n.locale = :es
        get :edit, {:id => 3, :locale => :es}
        response.body.should include('Baguette')
        I18n.locale = :en
    end

    it "saves edits to a public body" do
        I18n.locale = :es
        pb = PublicBody.find(id=3)
        pb.name.should == "El Department for Humpadinking"
        post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }}
        response.flash[:notice].should include('successful') 
        I18n.locale = :en

        pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
        PublicBody.with_locale(:es) do
           pb.name.should == "Renamed"
        end
        PublicBody.with_locale(:en) do
           pb.name.should == "Department for Humpadinking"
        end
    end

    it "destroy a public body" do
        PublicBody.count.should == 2
        post :destroy, { :id => 3 }
        PublicBody.count.should == 1
    end

end