Skip to content

Commit

Permalink
Verify expiry date-time during upload (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel committed Dec 23, 2018
1 parent c622799 commit bc17cc1
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Tus/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ public function getChecksumAlgorithm() : string
return $this->checksumAlgorithm;
}

/**
* Check if current upload is expired.
*
* @return bool
*/
public function isExpired() : bool
{
$expiresAt = $this->getCache()->get($this->getKey())['expires_at'] ?? null;

return empty($expiresAt) || Carbon::parse($expiresAt)->lt(Carbon::now());
}

/**
* Check if this is a partial upload request.
*
Expand Down Expand Up @@ -283,6 +295,11 @@ public function upload(int $bytes = -1) : int
throw new ConnectionException("Couldn't connect to server.");
}

// Verify that upload is not yet expired.
if ($this->isExpired()) {
throw new TusException('Upload expired.');
}

// Now, resume upload with PATCH request.
return $this->sendPatchRequest($bytes, $offset);
}
Expand Down
112 changes: 112 additions & 0 deletions tests/Tus/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,72 @@ public function it_generates_unique_key_for_partial_checksum()
$this->assertTrue($this->tusClientMock->isPartial());
}

/**
* @test
*
* @covers ::isExpired
*/
public function it_returns_false_if_upload_is_not_expired()
{
$key = uniqid();
$url = 'https://server.tus.local';

$cacheMock = m::mock(FileStore::class);
$cacheMock
->shouldReceive('get')
->once()
->with($key)
->andReturn([
'location' => $url,
'expires_at' => 'Sat, 09 Dec 2017 00:00:00 GMT',
]);

$this->tusClientMock
->shouldReceive('getKey')
->once()
->andReturn($key);

$this->tusClientMock
->shouldReceive('getCache')
->once()
->andReturn($cacheMock);

$this->assertFalse($this->tusClientMock->isExpired());
}

/**
* @test
*
* @covers ::isExpired
*/
public function it_returns_true_if_upload_is_expired()
{
$key = uniqid();
$url = 'https://server.tus.local';

$cacheMock = m::mock(FileStore::class);
$cacheMock
->shouldReceive('get')
->once()
->with($key)
->andReturn([
'location' => $url,
'expires_at' => 'Thu, 7 Dec 2017 00:00:00 GMT',
]);

$this->tusClientMock
->shouldReceive('getKey')
->once()
->andReturn($key);

$this->tusClientMock
->shouldReceive('getCache')
->once()
->andReturn($cacheMock);

$this->assertTrue($this->tusClientMock->isExpired());
}

/**
* @test
*
Expand All @@ -337,6 +403,11 @@ public function it_uploads_a_file()
->once()
->andReturn(0);

$this->tusClientMock
->shouldReceive('isExpired')
->once()
->andReturn(false);

$this->tusClientMock
->shouldReceive('sendPatchRequest')
->once()
Expand Down Expand Up @@ -366,6 +437,11 @@ public function it_uploads_full_file_if_size_is_not_given()
->once()
->andReturn(0);

$this->tusClientMock
->shouldReceive('isExpired')
->once()
->andReturn(false);

$this->tusClientMock
->shouldReceive('sendPatchRequest')
->once()
Expand All @@ -375,6 +451,32 @@ public function it_uploads_full_file_if_size_is_not_given()
$this->assertEquals($bytes, $this->tusClientMock->upload());
}

/**
* @test
*
* @expectedException \TusPhp\Exception\TusException
* @expectedExceptionMessage Upload expired.
*/
public function it_should_not_resume_upload_if_upload_is_expired()
{
$this->tusClientMock
->shouldReceive('getFileSize')
->once()
->andReturn(100);

$this->tusClientMock
->shouldReceive('sendHeadRequest')
->once()
->andReturn(0);

$this->tusClientMock
->shouldReceive('isExpired')
->once()
->andReturn(true);

$this->tusClientMock->upload();
}

/**
* @test
*
Expand All @@ -401,6 +503,11 @@ public function it_creates_and_then_uploads_a_file_in_file_exception()
->once()
->andReturn($key);

$this->tusClientMock
->shouldReceive('isExpired')
->once()
->andReturn(false);

$this->tusClientMock
->shouldReceive('sendPatchRequest')
->once()
Expand Down Expand Up @@ -436,6 +543,11 @@ public function it_creates_and_then_uploads_a_file_in_client_exception()
->once()
->andReturn($key);

$this->tusClientMock
->shouldReceive('isExpired')
->once()
->andReturn(false);

$this->tusClientMock
->shouldReceive('sendPatchRequest')
->once()
Expand Down

0 comments on commit bc17cc1

Please sign in to comment.