Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

WIP: SMS Flow tests #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/Console/Commands/SendScheduledMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Console\Commands;

use App\Subscriber;
use App\ScheduledMessage;
use Illuminate\Console\Command;
use App\Services\Sms\Contracts\Sms;
use App\Subscriber;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use \Twitter;
use Twitter;

class SendScheduledMessages extends Command
{
Expand Down Expand Up @@ -59,12 +59,13 @@ public function handle(Sms $sms)
$filteredSubscribers = $subscribers->filter(function ($sub) use ($locationTags, $topicTags) {
$locationTagMatches = $sub->locationTags->intersect($locationTags);
$topicTagMatches = $sub->topicTags->intersect($topicTags);

return $locationTagMatches->isNotEmpty() && $topicTagMatches->isNotEmpty();
});

// Send to filtered subscribers.
if ($filteredSubscribers->count()) {
Log::info('Sending message ' . $message->id . ' to ' . $filteredSubscribers->count() . ' subscribers.');
Log::info('Sending message '.$message->id.' to '.$filteredSubscribers->count().' subscribers.');

foreach ($filteredSubscribers as $subscriber) {
$body = $message->body_en;
Expand All @@ -82,7 +83,7 @@ public function handle(Sms $sms)
['scheduled_message_id' => $message->id]
);
} catch (\Exception $e) {
Log::info('Failed to send message ' . $message->id . ' to ' . $subscriber->number . ' (' . $subscriber->id . ')');
Log::info('Failed to send message '.$message->id.' to '.$subscriber->number.' ('.$subscriber->id.')');
}
}
}
Expand Down
1 change: 0 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Kernel extends ConsoleKernel
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
Expand Down
8 changes: 2 additions & 6 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ class Handler extends ExceptionHandler
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
* @return void
*/
public function report(Throwable $exception)
{
Expand All @@ -43,10 +41,8 @@ public function report(Throwable $exception)
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Throwable $exception)
{
Expand Down
3 changes: 0 additions & 3 deletions app/Filters/AbstractFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ abstract class AbstractFilters
*
* @var \Illuminate\Database\Eloquent\Builder
*/

protected $builder;

/**
* Create a new ThreadFilters instance.
*
* @param Request $request
*/
public function __construct(Request $request)
{
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
Expand Down Expand Up @@ -43,7 +43,6 @@ public function __construct()
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
Expand All @@ -58,7 +57,6 @@ protected function validator(array $data)
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
Expand Down
17 changes: 9 additions & 8 deletions app/Http/Controllers/Auth/SubscriberLoginController.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

namespace App\Http\Controllers\Auth;
use Carbon\Carbon;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Sms\Contracts\Sms;
use App\Subscriber;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Services\Sms\Contracts\Sms;

use App\Subscriber;

class SubscriberLoginController extends Controller
{
Expand Down Expand Up @@ -46,7 +45,7 @@ public function loginForm()

public function login(Request $request, Sms $sms)
{
$request->validate([ 'number' => 'required|digits:10' ]);
$request->validate(['number' => 'required|digits:10']);

$number = $request->get('number');

Expand All @@ -62,7 +61,7 @@ public function login(Request $request, Sms $sms)
if ($request->has('pledge')) {
$subscriber->pledged = true;
}
if ($request->has('referred_by') && !$subscriber->referred_by) {
if ($request->has('referred_by') && ! $subscriber->referred_by) {
$subscriber->referred_by = $request->input('referred_by');
}
if ($request->has('name')) {
Expand Down Expand Up @@ -95,13 +94,15 @@ public function verifyForm()
public function verify(Request $request)
{
if (Auth::guard('subscriber')->attempt($request->only(['number', 'password']))) {
if (!Auth::guard('subscriber')->user()->withinValidVerifyTime()) {
if (! Auth::guard('subscriber')->user()->withinValidVerifyTime()) {
return redirect()->back()->with('notify', 'Verification timed out, please try again.');
}

$request->session()->remove('fromPledge');

return redirect()->intended($this->redirectTo);
}

return redirect()->back()->with('notify', 'Login failed, give it another shot.');
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/ElectedOfficialsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;

Expand All @@ -29,7 +27,7 @@ public function index()
public function lookup(Request $request)
{
$validated = $request->validate([
'address' => 'required|string'
'address' => 'required|string',
]);

$client = new Client();
Expand All @@ -38,8 +36,8 @@ public function lookup(Request $request)
$response = $client->request('GET', $this->apiEndpoint, [
'query' => [
'address' => $validated['address'],
'key' => $this->apiKey
]
'key' => $this->apiKey,
],
]);

$data = Cache::remember($validated['address'], now()->addHour(), function () use ($response) {
Expand All @@ -61,9 +59,7 @@ public function lookup(Request $request)
});
}

throw ValidationException::withMessages([
'address' => 'We had some trouble reading that address. '.$reasons->implode('. ')
]);
throw ValidationException::withMessages(['address' => 'We had some trouble reading that address. '.$reasons->implode('. ')]);
}
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Http\Controllers;

use App\Subscriber;
use Exception;
use Illuminate\Support\Facades\Cache;
use App\Subscriber;

class HomeController extends Controller
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public function index()

public function pledge($referred_by = null)
{
return view('pledge', [ 'referred_by' => $referred_by ]);
return view('pledge', ['referred_by' => $referred_by]);
}

public function pledgeProgress()
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Controllers/ResourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ResourcesController extends Controller
{
/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ScheduledMessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function create(Request $request)
]);

$validator->after(function ($validator) use ($request) {
if (!Tag::validateRequiredTags($request->input('tags'))) {
if (! Tag::validateRequiredTags($request->input('tags'))) {
$validator->errors()->add('tags[]', 'Must select one of each tag type.');
}
});
Expand Down Expand Up @@ -97,7 +97,7 @@ public function update(ScheduledMessage $scheduled_message, Request $request)
]);

$validator->after(function ($validator) use ($request) {
if (!Tag::validateRequiredTags($request->input('tags'))) {
if (! Tag::validateRequiredTags($request->input('tags'))) {
$validator->errors()->add('tags[]', 'Must select one of each tag type.');
}
});
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Controllers/SmsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Controllers;

use App\Services\Sms\Contracts\Sms;
use App\Subscriber;
use App\Tag;
use Illuminate\Http\Request;
use App\Services\Sms\Contracts\Sms;

class SmsController extends Controller
{
Expand Down Expand Up @@ -58,6 +58,7 @@ public function receiveSms(Request $request, Sms $sms)
}

$subscriber->unsubscribe();

return $sms->response($subscriber->number, __('sms.unsubscribed', [], $subscriber->locale));
}

Expand All @@ -67,17 +68,17 @@ public function receiveSms(Request $request, Sms $sms)
// Not currently subscribed
$locale = $message->getLocaleFromTrigger('subscribe');
if ($locale) {
if (!$subscriber) {
if (! $subscriber) {
$subscriber = Subscriber::create([
'number' => $message->from,
'locale' => $locale
'locale' => $locale,
]);

$subscriber->tags()->sync(Tag::subscriberDefaults()->get());
}
$subscriber->update([
'subscribed' => true,
'locale' => $locale
'locale' => $locale,
]);

return $sms->response($subscriber->number, __('sms.subscribed', [], $subscriber->locale));
Expand Down
11 changes: 8 additions & 3 deletions app/Http/Controllers/SubscriberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace App\Http\Controllers;

use App\Filters\MessageFilters;
use App\Message;
use App\Subscriber;
use App\Tag;
use Illuminate\Http\Request;
use App\Filters\MessageFilters;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;

class SubscriberController extends Controller
{
Expand All @@ -29,6 +29,7 @@ public function new()
public function home()
{
$subscriber = Auth::guard('subscriber')->user();

return view('subscriber.home', [
'subscriber' => $subscriber,
'locationTags' => Tag::locations()->get(),
Expand Down Expand Up @@ -85,7 +86,7 @@ public function update(Request $request, Subscriber $subscriber)

public function updateLocale(Request $request)
{
$request->validate([ 'locale' => 'required|in:en,es' ]);
$request->validate(['locale' => 'required|in:en,es']);

$subscriber = Auth::guard('subscriber')->user();
$subscriber->locale = $request->get('locale');
Expand All @@ -98,6 +99,7 @@ public function updateTags(Request $request)
{
$subscriber = Auth::guard('subscriber')->user();
$subscriber->tags()->sync($request->input('tags'));

return response()->json($subscriber->tagIds());
}

Expand All @@ -106,6 +108,7 @@ public function enable()
$subscriber = Auth::guard('subscriber')->user();
$subscriber->subscribed = true;
$subscriber->save();

return redirect()->route('subscriber.home');
}

Expand All @@ -114,6 +117,7 @@ public function disable()
$subscriber = Auth::guard('subscriber')->user();
$subscriber->subscribed = false;
$subscriber->save();

return redirect()->route('subscriber.home');
}

Expand Down Expand Up @@ -148,6 +152,7 @@ public function pledgeDisplayUpdate(Request $request)
$subscriber = Auth::guard('subscriber')->user();
$subscriber->hide_from_pledge_board = $request->boolean('hide_from_pledge_board');
$subscriber->save();

return redirect()->route('subscriber.home')->with('status', 'Pledge board display status updated.');
}
}
Loading