aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/admin/users_import.t
blob: df8884797d5d119b2aa4abb5cd316ade414c4609 (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
use FixMyStreet::TestMech;

my $mech = FixMyStreet::TestMech->new;

my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1);
my $body = $mech->create_body_ok(2509, 'Haringey Borough Council');

$mech->log_in_ok( $superuser->email );

my $body_id = $body->id;
my $csv = <<EOF;
name,email,from_body,permissions,roles
Adrian,adrian\@example.org,$body_id,moderate:user_edit,
Belinda,belinda\@example.org,$body_id,,Customer Service
EOF

FixMyStreet::DB->resultset("Role")->create({
    body => $body,
    name => 'Customer Service',
});

subtest 'import CSV file' => sub {
    $mech->get_ok('/admin/users/import');
    $mech->submit_form_ok({ with_fields => {
        csvfile => [ [ undef, 'foo.csv', Content => $csv ], 1],
    }});
    $mech->content_contains('Created 2 new users');
    my $a = FixMyStreet::DB->resultset("User")->find({ email => 'adrian@example.org' });
    is $a->user_body_permissions->count, 2;
    my $b = FixMyStreet::DB->resultset("User")->find({ email => 'belinda@example.org' });
    is $b->roles->count, 1;
};

done_testing();
ode>
# app/helpers/application_helper.rb:
# Methods added to this helper will be available to all views (i.e. templates)
# in the application.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: application_helper.rb,v 1.22 2008-07-09 07:24:50 francis Exp $
require 'languages'

module ApplicationHelper
    # URL generating functions are needed by all controllers (for redirects),
    # views (for links) and mailers (for use in emails), so include them into
    # all of all.
    include LinkToHelper

    # Site-wide access to configuration settings
    include ConfigHelper

    # Copied from error_messages_for in active_record_helper.rb
    def foi_error_messages_for(*params)
        options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
        objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
        count   = objects.inject(0) {|sum, object| sum + object.errors.count }
        unless count.zero?
            html = {}
            [:id, :class].each do |key|
              if options.include?(key)
                  value = options[key]
                  html[key] = value unless value.blank?
              else
                  html[key] = 'errorExplanation'
              end
          end
          
          error_messages = []
          for object in objects
              object.errors.each do |attr, message|
                  error_messages << content_tag(:li, message)
              end
          end
          
          content_tag(:div,
              content_tag(:ul, error_messages),
            html
          )
        else
            ''
        end
    end
    
    # Highlight words, also escapes HTML (other than spans that we add)
    def highlight_words(t, words, html = true)
        if html
            t = h(t)
        end
        if html
            t = highlight(t, words, '<span class="highlight">\1</span>')
        else
            t = highlight(t, words, '*\1*')
        end
        return t
    end
    def highlight_and_excerpt(t, words, excount, html = true)
        newt = excerpt(t, words[0], excount)
        if not newt
            newt = excerpt(t, '', excount)
        end
        t = newt
        t = highlight_words(t, words, html)
        return t
    end
	
    def locale_name(locale)
        return LanguageNames::get_language_name(locale)
    end  

    # Use our own algorithm for finding path of cache
    def foi_cache(name = {}, options = nil, &block)
        if @controller.perform_caching
            key = name.merge(:only_path => true)
            key_path = @controller.foi_fragment_cache_path(key)

            if @controller.foi_fragment_cache_exists?(key_path)
                cached = @controller.foi_fragment_cache_read(key_path)
                output_buffer.concat(cached)
                return
            end

            pos = output_buffer.length
            content = block.call
            @controller.foi_fragment_cache_write(key_path, output_buffer[pos..-1])
        else
            block.call
        end
    end
end