Skip to content

Commit

Permalink
refactor: change getAlgorithmById from static to instance to make it …
Browse files Browse the repository at this point in the history
…testable

Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
  • Loading branch information
ernolf committed Aug 8, 2024
1 parent bd1990d commit ed19c43
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Service/ITotp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface ITotp {
public const DEFAULT_PERIOD = 30; // Period in seconds


public static function getAlgorithmById(int $id): string;
public function getAlgorithmById(int $id): string;

public function hasSecret(IUser $user): bool;

Expand Down
4 changes: 2 additions & 2 deletions lib/Service/Totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getDefaultDigits(): int {
return ($length >= ITotp::DEFAULT_DIGITS && $length <= self::MAX_DIGITS) ? $length : ITotp::DEFAULT_DIGITS;
}

public static function getAlgorithmById(int $id): string {
public function getAlgorithmById(int $id): string {
switch ($id) {
case ITotp::HASH_SHA1:
return \EasyTOTP\TOTPInterface::HASH_SHA1;
Expand Down Expand Up @@ -275,7 +275,7 @@ public function validateSecret(IUser $user, string $key): bool {
}

$secret = $this->crypto->decrypt($dbSecret->getSecret());
$algorithm = self::getAlgorithmById($this->getAlgorithmId($user));
$algorithm = $this->getAlgorithmById($this->getAlgorithmId($user));
$digits = $this->getDigits($user);
$period = $this->getPeriod($user);
$otp = Factory::getTOTP(Base32::decode($secret), $period, $digits, 0, $algorithm);
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Controller/SettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public function testCreateSecret() {
->method('getDefaultDigits')
->willReturn(6);

// Mock the getAlgorithmById method
$this->totp->expects($this->once())
->method('getAlgorithmById')
->with(1) // Example value for algorithm
->willReturn('SHA1');

$issuer = rawurlencode($this->defaults->getName());
$qrUrl = "otpauth://totp/{$issuer}:user%40instance.com?secret=newsecret&issuer=$issuer&algorithm=SHA1&digits=6&period=30&image=";

Expand Down

0 comments on commit ed19c43

Please sign in to comment.