Skip to content

Commit 97ab5ac

Browse files
committed
add create email now fixing tests
1 parent 412ce01 commit 97ab5ac

File tree

7 files changed

+143
-2
lines changed

7 files changed

+143
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Whm::disableEmail('cPanel_username','[email protected]');
124124
Whm::enableEmail('cPanel_username','[email protected]');
125125
$whitelist = Whm::cphulkWhitelist();
126126
$blacklist = Whm::cphulkBlacklist();
127+
Whm::createEmail('[email protected]');
127128

128129
Whmcs::createClientGroup($name, $colour = '#ffffff');
129130
Whmcs::createCustomClientField($name, $type = 'text');

src/Contracts/WhmInterface.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ interface WhmInterface
66
{
77
public function bandwidth(): array;
88
public function suspendEmail(string $username, string $email): array;
9-
public function unsuspendEmail(string $username, string $email): array;
9+
public function unsuspendEmail(string $username, string $email): array;
10+
public function cphulkBlacklist(): array;
11+
public function cphulkWhitelist(): array;
12+
public function createEmail(
13+
string $cpanelUsername,
14+
string $email,
15+
string $password,
16+
?string $domain = null,
17+
?int $quota = null,
18+
bool $sendWelcomeEmail = false
19+
): array;
20+
public static function generatePassword(): string;
1021
}

src/Whm.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Eugenefvdm\Api\Contracts\WhmInterface;
66
use Illuminate\Http\Client\PendingRequest;
77
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Str;
89

910
class Whm implements WhmInterface
1011
{
@@ -105,7 +106,7 @@ public function suspendEmail(string $cpanelUsername, string $email): array
105106

106107
/**
107108
* Unsuspend an email account's login ability
108-
* @link https://api.docs.cpanel.net/openapi/cpanel/operation/suspend_login/
109+
* @link https://api.docs.cpanel.net/openapi/cpanel/operation/unsuspend_login/
109110
* @param string $email The email address to unsuspend
110111
* @param string $cpanelUsername The cPanel username that owns the email account
111112
* @return array Response from the API with HTTP status code
@@ -176,6 +177,70 @@ public function cphulkWhitelist(): array
176177
return $this->client()->get('/json-api/read_cphulk_records?api.version=1&list_name=white')->json();
177178
}
178179

180+
/**
181+
* Create a new email account
182+
* @link https://api.docs.cpanel.net/openapi/cpanel/operation/add_pop/ WHM API Documentation for add_pop
183+
* @param string $cpanelUsername The cPanel username that owns the email account
184+
* @param string $email The email account username (without domain)
185+
* @param string $password The email account password
186+
* @param string|null $domain The domain for the email account (defaults to account's main domain)
187+
* @param int|null $quota The maximum disk space in MB (0 for unlimited)
188+
* @param bool $sendWelcomeEmail Whether to send client configuration instructions
189+
* @return array Response from the API with HTTP status code
190+
*/
191+
public function createEmail(
192+
string $cpanelUsername,
193+
string $email,
194+
string $password,
195+
?string $domain = null,
196+
?int $quota = null,
197+
bool $sendWelcomeEmail = false
198+
): array {
199+
$params = [
200+
'cpanel_jsonapi_apiversion' => 3,
201+
'cpanel_jsonapi_user' => $cpanelUsername,
202+
'cpanel_jsonapi_module' => 'Email',
203+
'cpanel_jsonapi_func' => 'add_pop',
204+
'email' => $email,
205+
'password' => $password,
206+
];
207+
208+
if ($domain !== null) {
209+
$params['domain'] = $domain;
210+
}
211+
212+
if ($quota !== null) {
213+
$params['quota'] = $quota;
214+
}
215+
216+
if ($sendWelcomeEmail) {
217+
$params['send_welcome_email'] = 1;
218+
}
219+
220+
$response = $this->client()->get('/json-api/cpanel', $params)->json();
221+
222+
// Check for errors
223+
if (isset($response['result']['errors'])) {
224+
return [
225+
'status' => 'error',
226+
'code' => 400,
227+
'output' => $response['result']['errors'][0] ?? 'Unknown error occurred'
228+
];
229+
}
230+
231+
// Success case
232+
return [
233+
'status' => 'success',
234+
'code' => 200,
235+
'output' => $response['result']['data'] ?? [],
236+
];
237+
}
238+
239+
public static function generatePassword(): string
240+
{
241+
return Str::random(12);
242+
}
243+
179244
/**
180245
* Set the HTTP client (used for testing)
181246
*

tests/Feature/WhmTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,52 @@
108108
expect($result['data']['ips_in_list'])->toHaveKey('1.2.3.4');
109109
expect($result['data']['ips_in_list'])->toHaveKey('4.5.106.198');
110110
});
111+
112+
test('it can create an email account successfully', function () {
113+
$whm = mock(WhmInterface::class);
114+
115+
$whm->shouldReceive('createEmail')
116+
->with('username', 'user', 'password123', null, null, false)
117+
->andReturn(json_decode(file_get_contents(__DIR__.'/../stubs/whm/create_email_success.json'), true));
118+
119+
$result = $whm->createEmail('username', 'user', 'password123');
120+
121+
expect($result['status'])->toBe('success');
122+
expect($result['code'])->toBe(200);
123+
expect($result['output'])->toBe('user+example.com');
124+
})->only();
125+
126+
test('it returns 400 when email account already exists', function () {
127+
$whm = mock(WhmInterface::class);
128+
129+
$whm->shouldReceive('createEmail')
130+
->with('username', 'user', 'password123', null, null, false)
131+
->andReturn(json_decode(file_get_contents(__DIR__.'/../stubs/whm/create_email_already_exists.json'), true));
132+
133+
$result = $whm->createEmail('username', 'user', 'password123');
134+
135+
expect($result['status'])->toBe('error');
136+
expect($result['code'])->toBe(400);
137+
expect($result['output'])->toBe('The account [email protected] already exists!');
138+
});
139+
140+
test('it returns 400 when password strength is too weak', function () {
141+
$whm = mock(WhmInterface::class);
142+
143+
$whm->shouldReceive('createEmail')
144+
->with('username', 'user', 'weakpass', null, null, false)
145+
->andReturn(json_decode(file_get_contents(__DIR__.'/../stubs/whm/create_email_password_strengh_issue.json'), true));
146+
147+
$result = $whm->createEmail('username', 'user', 'weakpass');
148+
149+
expect($result['status'])->toBe('error');
150+
expect($result['code'])->toBe(400);
151+
expect($result['output'])->toBe('The password that you entered has a strength rating of "1". You cannot use it because it is too weak and too easy to guess. Please enter a password with a strength rating of "65" or higher.');
152+
});
153+
154+
test('generatePassword returns a 12 character string', function () {
155+
$password = Whm::generatePassword();
156+
157+
expect($password)->toBeString()
158+
->toHaveLength(12);
159+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"status": "error",
3+
"code": 400,
4+
"output": "The account [email protected] already exists!"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"status": "error",
3+
"code": 400,
4+
"output": "The password that you entered has a strength rating of “1”. You cannot use it because it is too weak and too easy to guess. Please enter a password with a strength rating of “65” or higher."
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"status": "success",
3+
"code": 200,
4+
"output": "user+example.com"
5+
}

0 commit comments

Comments
 (0)