<commit_before><?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Models\User;
use App\Models\Role;
use App\Models\Application;

class ApplicationTest extends TestCase
{
  use DatabaseTransactions;

  //Mock a sample user and get a token
  public function getAuthToken()
  {
    //Register a sample user
    $user = factory(\App\Models\User::class)->create([
      'firstname' => 'Test',
      'lastname' => 'User',
      'email' => '<EMAIL>',
    ]);
    $response = $this->call('POST','/api/user/auth',['email' => '<EMAIL>', 'password' => '<PASSWORD>']);
    return json_decode($response->getContent(),true)['token'];
  }

  //Mock a sample user and get a token
  public function getAdminAuthToken()
  {
    //Register a sample user
    $user = factory(\App\Models\User::class)->create([
      'firstname' => 'Test',
      'lastname' => 'Admin',
      'email' => '<EMAIL>',
    ]);
    // $user->roles()->attach(Role::where('name','admin')->first()->id);
    $response = $this->call('POST','/api/user/auth',['email' => '<EMAIL>', 'password' => '<PASSWORD>']);
    return json_decode($response->getContent(),true)['token'];
  }

  public function setUp() {
    parent::setUp();
    $this->token = ApplicationTest::getAuthToken();
    $this->user = User::where('email','<EMAIL>')->first();
    $this->adminToken = ApplicationTest::getAdminAuthToken();
    $this->adminUser = User::where('email','<EMAIL>')->first();
    $this->adminUser->roles()->attach(Role::where('name','admin')->first()->id);
    $this->adminUser->roles()->attach(Role::where('name','devteam')->first()->id);
  }

  public function buildValidApp() {
    //Create a sample valid application
    $appData = [
      'class_year' => 'freshman',
      'grad_year' => '2021',
      'major' => 'Computer Science',
      'referral' => 'none',
      'hackathon_count' => 0,
      'shirt_size' => 'l',
      'dietary_restrictions' => 'l',
      'website' => 'http://github.com',
      'longanswer_1' => 'Sample question 1',
      'longanswer_2' => 'Sample question 2',
    ];
    return $appData;
  }

  public function testSubmitApplication() {
    $appData = ApplicationTest::buildValidApp();
    $appData['major'] = ""; //Invalid major

    //Check application with unverified email
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'unverified_email']);

    //Verify user email
    $this->user->verified = true;
    $this->user->save();

    //Check invalid application
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'validation']);
    $this->assertDatabaseMissing('applications',['user_id' => $this->user->id]);

    //Check that application fails if they're closed
    putenv("APPLICATION_MODE=CLOSED");
    $appData = ApplicationTest::buildValidApp();
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJsonFragment(['message' => 'validation','application_mode' => ['Applications are not open']]);
    $this->assertDatabaseMissing('applications',['user_id' => $this->user->id]);
    putenv("APPLICATION_MODE=OPEN");


    //Check valid application
    $appData = ApplicationTest::buildValidApp();<commit_msg>yay test coverage<commit_after><?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Models\User;
use App\Models\Role;
use App\Models\Application;

class ApplicationTest extends TestCase
{
  use DatabaseTransactions;

  //Mock a sample user and get a token
  public function getAuthToken()
  {
    //Register a sample user
    $user = factory(\App\Models\User::class)->create([
      'firstname' => 'Test',
      'lastname' => 'User',
      'email' => '<EMAIL>',
    ]);
    $response = $this->call('POST','/api/user/auth',['email' => '<EMAIL>', 'password' => '<PASSWORD>']);
    return json_decode($response->getContent(),true)['token'];
  }

  //Mock a sample user and get a token
  public function getAdminAuthToken()
  {
    //Register a sample user
    $user = factory(\App\Models\User::class)->create([
      'firstname' => 'Test',
      'lastname' => 'Admin',
      'email' => '<EMAIL>',
    ]);
    // $user->roles()->attach(Role::where('name','admin')->first()->id);
    $response = $this->call('POST','/api/user/auth',['email' => '<EMAIL>', 'password' => '<PASSWORD>']);
    return json_decode($response->getContent(),true)['token'];
  }

  public function setUp() {
    parent::setUp();
    $this->token = ApplicationTest::getAuthToken();
    $this->user = User::where('email','<EMAIL>')->first();
    $this->adminToken = ApplicationTest::getAdminAuthToken();
    $this->adminUser = User::where('email','<EMAIL>')->first();
    $this->adminUser->roles()->attach(Role::where('name','admin')->first()->id);
    $this->adminUser->roles()->attach(Role::where('name','devteam')->first()->id);
  }

  public function buildValidApp() {
    //Create a sample valid application
    $appData = [
      'class_year' => 'freshman',
      'grad_year' => '2021',
      'major' => 'Computer Science',
      'referral' => 'none',
      'hackathon_count' => 0,
      'shirt_size' => 'l',
      'dietary_restrictions' => 'l',
      'website' => 'http://github.com',
      'longanswer_1' => 'Sample question 1',
      'longanswer_2' => 'Sample question 2',
    ];
    return $appData;
  }

  public function testSubmitApplication() {
    $appData = ApplicationTest::buildValidApp();
    $appData['major'] = ""; //Invalid major

    //Check application with unverified email
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'unverified_email']);

    //Check verification route
    $this->actingAs($this->user)
    ->get('api/user/isverified',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'success', 'verified' => 0]);

    //Verify user email
    $this->user->verified = true;
    $this->user->save();

    //Check verification route
    $this->actingAs($this->user)
    ->get('api/user/isverified',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'success', 'verified' => 1]);

    //Check invalid application
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJson(['message' => 'validation']);
    $this->assertDatabaseMissing('applications',['user_id' => $this->user->id]);

    //Check that application fails if they're closed
    putenv("APPLICATION_MODE=CLOSED");
    $appData = ApplicationTest::buildValidApp();
    $this->actingAs($this->user)
    ->post('api/user/apply',$appData,
    ['Authorization' => 'Bearer '.$this->token])
    ->assertJsonFragment(['message' => 'validation','application_mode' => ['Applications are not open']]);
    $this->assertDatabaseMissing('applications',['user_id' => $this->user->id]);
    putenv("APPLICATION_MODE=OPEN");


    //Check valid application
    $appData = ApplicationTest::buildValidApp();
{'fraction_non_alphanumeric': 0.20287539936102236, 'fraction_numerical': 0.0026624068157614484, 'mean_word_length': 3.965631196298744, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 195, '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': '6254833', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2315, 'n_words': 427}