<commit_before>require 'rails_helper'

def stub_oauth_edit
  # Stub out the posting of content to Wikipedia using the same protocol as
  # wiki_edits_spec.rb
  # rubocop:disable Metrics/LineLength
  fake_tokens = "{\"query\":{\"tokens\":{\"csrftoken\":\"myfaketoken+\\\\\"}}}"
  # rubocop:enable Metrics/LineLength
  stub_request(:get, /.*wikipedia.*/)
    .to_return(status: 200, body: fake_tokens, headers: {})
  stub_request(:post, /.*wikipedia.*/)
    .to_return(status: 200, body: 'success', headers: {})
end

describe 'Student users', type: :feature, js: true do
  before do
    include Devise::TestHelpers, type: :feature
    Capybara.current_driver = :selenium
  end

  before :each do
    create(:cohort,
           id: 1)
    create(:course,
           id: 10001,
           slug: 'University/Course_(Term)',
           submitted: 1,
           listed: true,
           passcode: '<PASSWORD>',
           start: '2015-01-01'.to_date,
           end: '2020-01-01'.to_date)
    create(:cohorts_course,
           cohort_id: 1,
           course_id: 10001)
    user = create(:user,
                  id: 1,
                  wiki_token: 'foo',
                  wiki_secret: 'bar')
    login_as(user, scope: :user)
    visit root_path
  end

  describe 'enrolling and unenrolling by button' do
    it 'should join a course' do
      stub_oauth_edit

      # click enroll button
      visit "/courses/#{Course.first.slug}"
      first('button').click

      # enter passcode in alert popup to enroll
      prompt = page.driver.browser.switch_to.alert
      prompt.send_keys('<PASSWORD>')
      prompt.accept

      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).to have_content User.first.wiki_id

      # now unenroll
      visit "/courses/#{Course.first.slug}"
      page.all('button').last.click
      page.driver.browser.switch_to.alert.accept

      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).not_to have_content User.first.wiki_id
    end
  end

  describe 'enrolling by url' do
    it 'should join a course' do
      stub_oauth_edit

      visit "/courses/#{Course.first.slug}/students/enroll/passcode"
      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).to have_content User.first.wiki_id
    end
  end

  describe 'adding and removing assignments' do
    it 'should work' do
      stub_oauth_edit
      create(:courses_user,
             course_id: 10001,
             user_id: 1,
             role: 0)
      visit "/courses/#{Course.first.slug}/students"

      # Add an assigned article
      first('button.border').click<commit_msg>Add a few sleep cycles to try to work around flaky feature spec<commit_after>require 'rails_helper'

def stub_oauth_edit
  # Stub out the posting of content to Wikipedia using the same protocol as
  # wiki_edits_spec.rb
  # rubocop:disable Metrics/LineLength
  fake_tokens = "{\"query\":{\"tokens\":{\"csrftoken\":\"myfaketoken+\\\\\"}}}"
  # rubocop:enable Metrics/LineLength
  stub_request(:get, /.*wikipedia.*/)
    .to_return(status: 200, body: fake_tokens, headers: {})
  stub_request(:post, /.*wikipedia.*/)
    .to_return(status: 200, body: 'success', headers: {})
end

describe 'Student users', type: :feature, js: true do
  before do
    include Devise::TestHelpers, type: :feature
    Capybara.current_driver = :selenium
  end

  before :each do
    create(:cohort,
           id: 1)
    create(:course,
           id: 10001,
           slug: 'University/Course_(Term)',
           submitted: 1,
           listed: true,
           passcode: '<PASSWORD>',
           start: '2015-01-01'.to_date,
           end: '2020-01-01'.to_date)
    create(:cohorts_course,
           cohort_id: 1,
           course_id: 10001)
    user = create(:user,
                  id: 1,
                  wiki_token: 'foo',
                  wiki_secret: 'bar')
    login_as(user, scope: :user)
    visit root_path
  end

  describe 'enrolling and unenrolling by button' do
    it 'should join a course' do
      stub_oauth_edit

      # click enroll button
      visit "/courses/#{Course.first.slug}"
      sleep 1
      first('button').click

      # enter passcode in alert popup to enroll
      prompt = page.driver.browser.switch_to.alert
      prompt.send_keys('<PASSWORD>')
      prompt.accept

      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).to have_content User.first.wiki_id

      # now unenroll
      visit "/courses/#{Course.first.slug}"
      page.all('button').last.click
      page.driver.browser.switch_to.alert.accept

      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).not_to have_content User.first.wiki_id
    end
  end

  describe 'enrolling by url' do
    it 'should join a course' do
      stub_oauth_edit

      visit "/courses/#{Course.first.slug}/students/enroll/passcode"
      visit "/courses/#{Course.first.slug}/students"
      expect(first('tbody')).to have_content User.first.wiki_id
    end
  end

  describe 'adding and removing assignments' do
    it 'should work' do
      stub_oauth_edit
      create(:courses_user,
             course_id: 10001,
             user_id: 1,
             role: 0)
      visit "/courses/#{Course.first.slug}/students"
      sleep 1

      # Add an assigned article
      first('button.border').click
{'fraction_non_alphanumeric': 0.12308839985080194, 'fraction_numerical': 0.01641178664677359, 'mean_word_length': 2.480207657365347, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7467637', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1691, 'n_words': 397}