diff --git a/Gemfile.lock b/Gemfile.lock index 56045876..6e6aa062 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,15 +20,15 @@ GEM byebug (2.7.0) columnize (~> 0.3) debugger-linecache (~> 1.2) - capybara (2.4.1) + capybara (2.4.4) mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - capybara_minitest_spec (1.0.1) + capybara_minitest_spec (1.0.5) capybara (>= 2) - minitest (>= 2) + minitest (>= 4) celluloid (0.15.2) timers (~> 1.1.0) climate_control (0.0.3) @@ -99,8 +99,8 @@ GEM metaclass (0.0.4) method_source (0.8.2) mime-types (1.25.1) - mini_portile (0.6.0) - minitest (5.3.1) + mini_portile (0.6.2) + minitest (5.6.1) minitest-reporters (1.0.2) ansi builder @@ -108,14 +108,14 @@ GEM powerbar mocha (1.0.0) metaclass (~> 0.0.1) - multi_json (1.10.1) + multi_json (1.11.0) multipart-post (2.0.0) netrc (0.10.3) - nokogiri (1.6.3.1) - mini_portile (= 0.6.0) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) pg (0.17.1) phantomjs (1.9.7.1) - poltergeist (1.5.1) + poltergeist (1.6.0) capybara (~> 2.1) cliver (~> 0.3.1) multi_json (~> 1.0) @@ -133,14 +133,14 @@ GEM pry (~> 0.9.12) puma (2.8.1) rack (>= 1.1, < 2.0) - rack (1.5.2) + rack (1.6.0) rack-cache (1.2) rack (>= 0.4) rack-protection (1.5.2) rack rack-recaptcha (0.6.6) json - rack-test (0.6.2) + rack-test (0.6.3) rack (>= 1.0) rack_session_access (0.1.1) builder (>= 2.0.0) @@ -226,7 +226,9 @@ GEM webmock (1.17.4) addressable (>= 2.2.7) crack (>= 0.3.2) - websocket-driver (0.3.4) + websocket-driver (0.5.4) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.2) xpath (2.0.0) nokogiri (~> 1.3) zipruby (0.3.6) diff --git a/app/api.rb b/app/api.rb index b2907260..9a10751c 100644 --- a/app/api.rb +++ b/app/api.rb @@ -7,6 +7,7 @@ end post '/api/upload' do require_api_credentials + files = [] params.each do |k,v| next unless v.is_a?(Hash) && v[:tempfile] diff --git a/app/create.rb b/app/create.rb index 05403947..a9df8649 100644 --- a/app/create.rb +++ b/app/create.rb @@ -16,9 +16,11 @@ def new_recaptcha_valid? end end +CREATE_MATCH_REGEX = /^username$|^password$|^email$|^new_tags_string$|^is_education$/ + post '/create_validate_all' do content_type :json - fields = params.select {|p| p.match /^username$|^password$|^email$|^new_tags_string$/} + fields = params.select {|p| p.match CREATE_MATCH_REGEX} site = Site.new fields @@ -33,11 +35,12 @@ end post '/create_validate' do content_type :json - if !params[:field].match /^username$|^password$|^email$|^new_tags_string$/ + if !params[:field].match CREATE_MATCH_REGEX return {error: 'not a valid field'}.to_json - end + end site = Site.new(params[:field] => params[:value]) + site.is_education = params[:is_education] site.valid? field_sym = params[:field].to_sym @@ -58,7 +61,8 @@ post '/create' do username: params[:username], password: params[:password], email: params[:email], - new_tags_string: params[:tags], + new_tags_string: params[:new_tags_string], + is_education: params[:is_education] == 'true' ? true : false, ip: request.ip ) @@ -85,4 +89,4 @@ post '/create' do session[:id] = @site.id {result: 'ok'}.to_json -end \ No newline at end of file +end diff --git a/app/index.rb b/app/index.rb index d50ed3ea..eb7ec230 100644 --- a/app/index.rb +++ b/app/index.rb @@ -34,7 +34,7 @@ get '/?' do @sites_count = SimpleCache.get :sites_count end - erb :index, layout: false + erb :index, layout: :index_layout end get '/welcome' do @@ -44,7 +44,8 @@ get '/welcome' do end get '/education' do - erb :education, layout: false + redirect '/' if signed_in? + erb :education, layout: :index_layout end get '/tutorials' do diff --git a/migrations/064_add_education_to_sites.rb b/migrations/064_add_education_to_sites.rb new file mode 100644 index 00000000..f5c3fb48 --- /dev/null +++ b/migrations/064_add_education_to_sites.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + add_column :sites, :is_education, :boolean, default: false + } + + down { + drop_column :sites, :is_education + } +end diff --git a/models/site.rb b/models/site.rb index acc7def6..6833d00a 100644 --- a/models/site.rb +++ b/models/site.rb @@ -835,6 +835,18 @@ class Site < Sequel::Model new_tags.compact! @new_filtered_tags = [] + if values[:is_education] == true + if new? + if @new_tags_string.nil? || @new_tags_string.empty? + errors.add :new_tags_string, 'A Class Tag is required.' + end + + if new_tags.length > 1 + errors.add :new_tags_string, 'Must only have one tag' + end + end + end + if ((new? ? 0 : tags_dataset.count) + new_tags.length > 5) errors.add :new_tags_string, 'Cannot have more than 5 tags for your site.' end @@ -861,7 +873,7 @@ class Site < Sequel::Model break end - next if tags.collect {|t| t.name}.include? tag + next if !new? && tags.collect {|t| t.name}.include?(tag) @new_filtered_tags << tag @new_filtered_tags.uniq! diff --git a/tests/acceptance/education_tests.rb b/tests/acceptance/education_tests.rb new file mode 100644 index 00000000..449890ca --- /dev/null +++ b/tests/acceptance/education_tests.rb @@ -0,0 +1,61 @@ +require_relative './environment.rb' + +Capybara.register_driver :poltergeist do |app| + Capybara::Poltergeist::Driver.new(app, js_errors: false) +end + +describe 'signup' do + include Capybara::DSL + + def fill_in_valid + @site = Fabricate.attributes_for(:site) + @class_tag = SecureRandom.uuid.gsub('-', '')[0..Tag::NAME_LENGTH_MAX-1] + fill_in 'username', with: @site[:username] + fill_in 'password', with: @site[:password] + fill_in 'email', with: @site[:email] + fill_in 'new_tags_string', with: @class_tag + end + + before do + Capybara.default_driver = :poltergeist + Capybara.reset_sessions! + visit '/education' + page.must_have_content 'Neocities' # Used to force load wait + end + + after do + Capybara.default_driver = :rack_test + end + + it 'succeeds with valid data' do + fill_in_valid + click_button 'Create My Site' + page.must_have_content 'Welcome to Neocities' + + index_file_path = File.join Site::SITE_FILES_ROOT, @site[:username], 'index.html' + File.exist?(index_file_path).must_equal true + + site = Site[username: @site[:username]] + site.site_files.length.must_equal 4 + site.site_changed.must_equal false + site.site_updated_at.must_equal nil + site.is_education.must_equal true + site.tags.length.must_equal 1 + site.tags.first.name.must_equal @class_tag + end + + it 'fails to create for existing site' do + @existing_site = Fabricate :site + fill_in_valid + fill_in :username, with: @existing_site.username + click_button 'Create My Site' + page.must_have_content 'already taken' + end + + it 'fails for multiple tags' do + fill_in_valid + fill_in :new_tags_string, with: 'derp, ie' + click_button 'Create My Site' + page.must_have_content 'Must only have one tag' + end +end diff --git a/tests/acceptance/environment.rb b/tests/acceptance/environment.rb index b0f3379b..352aeafd 100644 --- a/tests/acceptance/environment.rb +++ b/tests/acceptance/environment.rb @@ -4,4 +4,6 @@ Capybara.app = Sinatra::Application def teardown Capybara.reset_sessions! -end \ No newline at end of file +end + +Capybara.default_wait_time = 5 diff --git a/tests/acceptance/signup_tests.rb b/tests/acceptance/signup_tests.rb index 6e2a6719..d5018b26 100644 --- a/tests/acceptance/signup_tests.rb +++ b/tests/acceptance/signup_tests.rb @@ -30,6 +30,7 @@ describe 'signup' do Capybara.default_driver = :poltergeist Capybara.reset_sessions! visit_signup + page.must_have_content 'Neocities' # Used to force load wait end after do @@ -48,19 +49,15 @@ describe 'signup' do site.site_files.length.must_equal 4 site.site_changed.must_equal false site.site_updated_at.must_equal nil + site.is_education.must_equal false site.ip.must_equal Site.hash_ip('127.0.0.1') end it 'fails to create for existing site' do + @existing_site = Fabricate :site fill_in_valid - click_signup_button - page.must_have_content 'Welcome to Neocities' - Capybara.reset_sessions! - visit_signup - sleep 0.3 - fill_in 'username', with: @site[:username] - fill_in 'password', with: @site[:password] + fill_in 'username', with: @existing_site.username click_signup_button page.must_have_content 'already taken' end @@ -113,9 +110,6 @@ describe 'signup' do page.must_have_content /email.+exists/ end -puts "$$$$$$$$$$$$$$$$$$$$$$ TODO FIX TAGS TESTS" - -=begin it 'succeeds with no tags' do fill_in_valid fill_in 'new_tags_string', with: '' @@ -139,7 +133,7 @@ puts "$$$$$$$$$$$$$$$$$$$$$$ TODO FIX TAGS TESTS" Site.last.tags.collect {|t| t.name}.must_equal ['derpie', 'shoujo'] end - it 'fails with invalid tag chars' do + it 'fails with invalid tag chars' do fill_in_valid fill_in 'new_tags_string', with: '$POLICE OFFICER$$$$$, derp' click_signup_button @@ -179,9 +173,10 @@ puts "$$$$$$$$$$$$$$$$$$$$$$ TODO FIX TAGS TESTS" fill_in 'new_tags_string', with: 'one, one' click_signup_button - site = Site.last + page.must_have_content /Welcome to Neocities/ + + site = Site[username: @site[:username]] site.tags.length.must_equal 1 site.tags.first.name.must_equal 'one' end -=end end diff --git a/tests/api_tests.rb b/tests/api_tests.rb index 5f94665a..6eaedc66 100644 --- a/tests/api_tests.rb +++ b/tests/api_tests.rb @@ -167,14 +167,6 @@ describe 'api upload' do '/' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') } res[:error_type].must_equal 'invalid_file_type' - - create_site - basic_authorize @user, @pass - post '/api/upload', { - '' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') - } - - res[:error_type].must_equal 'missing_files' end it 'fails for file with no extension' do diff --git a/views/_index_signup_script.erb b/views/_index_signup_script.erb new file mode 100644 index 00000000..71939735 --- /dev/null +++ b/views/_index_signup_script.erb @@ -0,0 +1,37 @@ + + + diff --git a/views/education.erb b/views/education.erb index 3b9f7c3c..15a1082d 100644 --- a/views/education.erb +++ b/views/education.erb @@ -1,189 +1,148 @@ - - - - - - + + - Neocities for Education - - - - +
- +
- - - - - + + <%== erb :'_index_signup_script', layout: false %> + diff --git a/views/index_layout.erb b/views/index_layout.erb new file mode 100644 index 00000000..25a7c874 --- /dev/null +++ b/views/index_layout.erb @@ -0,0 +1,45 @@ + + + + + + + + Neocities: Create your free website now! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +<%== yield %> + +