Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slack In and Out
server {
listen 80 default_server;
return 301 https://$host$request_uri;
}
# HTTPS server
server {
listen 443 default_server;
root /home/{{ user }}/app/public/;
index index.html index.htm index.php;
####
# We use these since the ELB is taking care of CERTS
####
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_protocols TLSv1.1 TLSv1.2;
charset utf-8;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.php?$query_string;
## This caused the root page not to work
## eg no ending slash
#try_files $uri $uri/index.php /index.php?$query_string;
auth_basic "Restricted";
auth_basic_user_file /home/catuser/htpasswd;
}
location /api/v1/slack { auth_basic "off"; allow all; satisfy any; try_files $uri $uri/ /index.php?$query_string; }
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_log /var/log/nginx/error.log;
error_page 403 =404 /404.html;
access_log /var/log/nginx/access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 700;
keepalive_timeout 700;
types_hash_max_size 2048;
}
location ~ /\.ht {
deny all;
}
}
<?php
use App\Exceptions\NotSlackUrlException;
use App\Jobs\RunHubplannerReportJob;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Queue;
class RunReportSlackTest extends TestCase
{
use \App\SlackTrait;
/** @var \Illuminate\Http\Request */
protected $request;
public function setUp()
{
parent::setUp();
$request = Mockery::mock(\Illuminate\Http\Request::class);
$this->request = $request;
}
/**
* @expectedException \Exception
*/
public function testVerifiesToken()
{
$this->doesntExpectJobs(RunHubplannerReportJob::class);
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$this->request->shouldReceive('get')->andReturnFalse();
$report->handle($this->request);
}
/**
* @expectedException \App\Exceptions\WrongUserException
*/
public function testSuccessVerifiesToken()
{
$this->doesntExpectJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->andReturn('foo');
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setToken('foo');
$report->handle($this->request);
}
/**
* @expectedException \App\Exceptions\WrongUserException
*/
public function testFailVerifiesPerson()
{
$this->doesntExpectJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->andReturn('foo');
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setToken('foo');
$report->handle($this->request);
}
public function testSuccessSlackURL()
{
$this->expectsJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->with('token')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('text')->andReturn('update_drive=false foo')->once();
$this->request->shouldReceive('get')->with('user_name')->andReturn('alfrednutile')->once();
$this->request->shouldReceive('get')->with('response_url')->andReturn('https://hooks.slack.com/commands/foo/bar/baz')->once();
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setAllowedUsers(['alfrednutile']);
$report->setToken('foo');
PHPUnit_Framework_Assert::assertTrue($report->handle($this->request));
}
/**
* @expectedException \App\Exceptions\NotSlackUrlException
*/
public function testFailWrongURL()
{
$this->doesntExpectJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->with('token')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('user_name')->andReturn('alfrednutile')->once();
$this->request->shouldReceive('get')->with('response_url')->andReturn('http://baz.foo.com')->once();
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setAllowedUsers(['alfrednutile']);
$report->setToken('foo');
PHPUnit_Framework_Assert::assertTrue($report->handle($this->request));
}
public function testKeepSheetsAsTrue()
{
$this->expectsJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->with('token')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('text')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('user_name')->andReturn('alfrednutile')->once();
$this->request->shouldReceive('get')->with('response_url')->andReturn('https://hooks.slack.com/commands/foo/bar/baz')->once();
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setAllowedUsers(['alfrednutile']);
$report->setToken('foo');
$report->handle($this->request);
PHPUnit_Framework_Assert::assertTrue($report->getJob()->update_drive);
}
public function testAppendsNoSheetUpdateToRequest()
{
$this->expectsJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->with('token')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('text')->andReturn('update_drive=false foo')->once();
$this->request->shouldReceive('get')->with('user_name')->andReturn('alfrednutile')->once();
$this->request->shouldReceive('get')->with('response_url')->andReturn('https://hooks.slack.com/commands/foo/bar/baz')->once();
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setAllowedUsers(['alfrednutile']);
$report->setToken('foo');
$report->handle($this->request);
PHPUnit_Framework_Assert::assertFalse($report->getJob()->update_drive);
}
public function testSuccessVerifiesPerson()
{
$this->expectsJobs(RunHubplannerReportJob::class);
$fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
$fixture = json_decode($fixture, true);
$this->request->shouldReceive('all')->andReturn($fixture);
$this->request->shouldReceive('get')->with('token')->andReturn('foo')->once();
$this->request->shouldReceive('get')->with('user_name')->andReturn('alfrednutile')->once();
$this->request->shouldReceive('get')->with('text')->andReturn('update_drive=false foo')->once();
$this->request->shouldReceive('get')->with('response_url')->andReturn('https://hooks.slack.com/commands/foo/bar/baz')->once();
/** @var \App\PlannerSlackReport $report */
$report = new \App\PlannerSlackReport();
$report->setAllowedUsers(['alfrednutile']);
$report->setToken('foo');
PHPUnit_Framework_Assert::assertTrue($report->handle($this->request));
}
}
<?php
namespace App\Http\Controllers;
use App\Exceptions\NotSlackUrlException;
use App\SlackRequestForPersonalTimeSheetRepository;
use App\SlackTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
class RunUserLevelReportSlackController extends Controller
{
use SlackTrait;
/**
* @param Request $request
* @param SlackRequestForPersonalTimeSheetRepository $report
* @return mixed
*/
public function handle(Request $request, SlackRequestForPersonalTimeSheetRepository $report)
{
try
{
if(!$this->token) {
$this->setToken(env('SLACK_TOKEN_REPORT_REQUEST'));
}
if($this->notASlackUrl($request)) {
$message = sprintf("Not a Slack URL");
throw new NotSlackUrlException($message);
}
$report->handle($request);
/**
* Run the report from here
*/
$message = sprintf("Your report has been triggered. You will get an email when it is done");
Log::info(sprintf("%s", $message));
return Response::json($this->respondToSlack($message, null, $this->getMessageType()));
}
catch(\Exception $e)
{
Log::debug(sprintf("Error running personal report for user %s", $e->getMessage()));
return Response::json($this->respondToSlack("Error running personal report {$e->getMessage()}", null, 'in_channel'), 400);
}
}
}
[
{"token": "foo"},
{"team_id": "bar"},
{"team_domain": "baz"},
{"channel_id": "C0JKRVFOOBAR"},
{"channel_name": "test"},
{"user_id": "U02NC4UL6"},
{"user_name": "alfredfoo"},
{"command": "/report"},
{"text": "not really needed yet"},
{"response_url": "https://hooks.slack.com/commands/foo/bar/baz"}
]
<?php
namespace App;
use App\Exceptions\WrongUserException;
use Illuminate\Http\Request;
trait SlackTrait
{
public $message_type = 'in_channel';
protected $allowed_users = [];
public $token = false;
public function notASlackUrl(Request $request) {
$url = $request->get('response_url');
if(!isset($url) || !$this->slackUrl($url)) {
return true;
}
return false;
}
protected function slackUrl($url) {
$incoming = parse_url($url);
return ($incoming['host'] && $incoming['host'] == 'hooks.slack.com');
}
public function notTheSlackUserIAmLookingFor(Request $request) {
$user = $request->get('user_name');
if(!isset($user) || !$this->allowedUsers($user)) {
return true;
}
return false;
}
public function notTheSlackIAmLookingFor(Request $request) {
$token = $request->get('token');
if(!isset($token) || $this->getToken() != $token) {
return true;
}
return false;
}
public function searchForWordInText(Request $request, $word) {
return (strpos($request->get('text'), $word) !== false);
}
public function seeIfEphemeral($search)
{
if($pos = strpos($search, 'ephemeral'))
{
$search = str_replace('ephemeral', '', $search);
$this->message_type = 'ephemeral';
}
return $search;
}
public function setToken($token = false) {
if(!$token) {
$token = env('SLACK_TOKEN');
}
$this->token = $token;
}
public function getToken() {
if(!isset($this->token)) {
$this->setToken();
}
return $this->token;
}
public function getMessageType()
{
return $this->message_type;
}
public function respondToSlack($message, $attachment, $type = 'in_channel')
{
return ['response_type' => $type, 'text' => $message, 'attachments' => [ ['text' => $attachment ] ] ];
}
protected function allowedUsers($user) {
return in_array($user, $this->allowed_users);
}
public function setAllowedUsers(array $users) {
$this->allowed_users = $users;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment