-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package email | ||
|
||
// MailAddon is the struct used to unmarshall the addon response. | ||
type MailAddon struct { | ||
// ID is the ID of the addon | ||
ID int `json:"id"` | ||
// DiskSpace is the amount of disk space the addon provides | ||
DiskSpace int `json:"diskSpace"` | ||
// Mailboxes is the amount of extra mailboxes the addon provides | ||
Mailboxes int `json:"mailboxes"` | ||
// LinkedMailBox is the mailbox the addon is currently linked to, if any | ||
LinkedMailBox string `json:"linkedMailBox"` | ||
// CanBeLinked is whether this Addon is allowed to be linked | ||
CanBeLinked bool `json:"canBeLinked"` | ||
} | ||
|
||
// mailAddonWrapper struct contains a MailAddon in it, | ||
// this is solely used for unmarshalling/marshalling | ||
type mailAddonWrapper struct { | ||
MailAddons []MailAddon `json:"addons"` | ||
} | ||
|
||
// LinkAddonRequest is used to generate a json body that contains the Action, AddonID, and Mailbox properties | ||
type LinkAddonRequest struct { | ||
// Action could be linkmailbox or unlinkmailbox | ||
Action string `json:"action"` | ||
// AddonID is the id of the addon to link | ||
AddonID int `json:"addonId"` | ||
// Mailbox is the email address associated with the mailbox | ||
Mailbox string `json:"mailbox"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,3 +256,70 @@ func TestRepository_DeleteMaillist(t *testing.T) { | |
err := repo.DeleteMaillist("example.com", 1) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestRepository_LinkAddon(t *testing.T) { | ||
const expectedRequestBody = `{"action":"linkmailbox","addonId":7,"mailbox":"[email protected]"}` | ||
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody} | ||
client, tearDown := server.GetClient() | ||
defer tearDown() | ||
repo := Repository{Client: *client} | ||
|
||
err := repo.LinkMailaddon(7, "[email protected]") | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestRepository_LinkAddonInvalidMailbox(t *testing.T) { | ||
const expectedRequestBody = `{"action":"linkmailbox","addonId":7,"mailbox":"[email protected]"}` | ||
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody} | ||
client, tearDown := server.GetClient() | ||
defer tearDown() | ||
repo := Repository{Client: *client} | ||
|
||
err := repo.LinkMailaddon(7, "testexample.com") | ||
require.Error(t, err) | ||
expectedErrorMessage := "invalid mailbox" | ||
assert.EqualErrorf(t, err, expectedErrorMessage, "Error should be: %v, got: %v", expectedErrorMessage, err) | ||
} | ||
|
||
func TestRepository_UnlinkAddon(t *testing.T) { | ||
const expectedRequestBody = `{"action":"unlinkmailbox","addonId":7,"mailbox":"[email protected]"}` | ||
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody} | ||
client, tearDown := server.GetClient() | ||
defer tearDown() | ||
repo := Repository{Client: *client} | ||
|
||
err := repo.UnlinkMailaddon(7, "[email protected]") | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestRepository_UnlinkAddonInvalidMailbox(t *testing.T) { | ||
const expectedRequestBody = `{"action":"unlinkmailbox","addonId":7,"mailbox":"[email protected]"}` | ||
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody} | ||
client, tearDown := server.GetClient() | ||
defer tearDown() | ||
repo := Repository{Client: *client} | ||
|
||
err := repo.UnlinkMailaddon(7, "testexample.com") | ||
require.Error(t, err) | ||
expectedErrorMessage := "invalid mailbox" | ||
assert.EqualErrorf(t, err, expectedErrorMessage, "Error should be: %v, got: %v", expectedErrorMessage, err) | ||
} | ||
|
||
func TestRepository_GetMailAddonsByDomainName(t *testing.T) { | ||
|
||
const apiResponse = `{"addons": [{"id": 282154,"diskSpace": 1024,"mailboxes": 5,"linkedMailBox": "[email protected]","canBeLinked": false}]}` | ||
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse} | ||
client, tearDown := server.GetClient() | ||
defer tearDown() | ||
repo := Repository{Client: *client} | ||
|
||
all, err := repo.GetAddonsByDomainName("example.com") | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(all)) | ||
|
||
assert.Equal(t, 282154, all[0].ID) | ||
assert.Equal(t, "[email protected]", all[0].LinkedMailBox) | ||
assert.Equal(t, false, all[0].CanBeLinked) | ||
assert.Equal(t, 5, all[0].Mailboxes) | ||
assert.Equal(t, 1024, all[0].DiskSpace) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/transip/gotransip/v6" | ||
"github.com/transip/gotransip/v6/email" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
// Create a new client with the default demo client config, using the demo token | ||
client, err := gotransip.NewClient(gotransip.DemoClientConfiguration) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
emailRepo := email.Repository{Client: client} | ||
|
||
//transipdemo.net | ||
log.Println("Getting a list of email boxes") | ||
mailboxes, err := emailRepo.GetMailboxesByDomainName("transipdemo.net") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(strings.Repeat("-", 50)) | ||
for _, mailbox := range mailboxes { | ||
alias := mailbox.LocalPart | ||
if len(alias) == 0 { | ||
alias = "*" | ||
} | ||
fmt.Printf("Mailbox Identifier: %s, Available Disk Space: %d Status is %s \n", mailbox.Identifier, mailbox.AvailableDiskSpace, mailbox.Status) | ||
} | ||
fmt.Println(strings.Repeat("-", 50)) | ||
|
||
log.Println("Getting a list of email forwards") | ||
forwards, err := emailRepo.GetMailforwardsByDomainName("transipdemo.net") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(strings.Repeat("-", 50)) | ||
for _, forward := range forwards { | ||
alias := forward.LocalPart | ||
if len(alias) == 0 { | ||
alias = "*" | ||
} | ||
fmt.Printf("Forward ID %d Forwards to: '%s' whene receiving mail on %s@%s. Status is %s \n", forward.ID, forward.ForwardTo, alias, forward.Domain, forward.Status) | ||
} | ||
|
||
fmt.Println(strings.Repeat("-", 50)) | ||
|
||
log.Println("Getting a list of email lists") | ||
maillists, err := emailRepo.GetMaillistsByDomainName("transipdemo.net") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(strings.Repeat("-", 50)) | ||
for _, list := range maillists { | ||
fmt.Printf("List ID %d Name: '%s'. Email Address: %s \n", list.ID, list.Name, list.EmailAddress) | ||
} | ||
|
||
fmt.Println(strings.Repeat("-", 50)) | ||
|
||
log.Println("Getting a list of email addons") | ||
addons, err := emailRepo.GetAddonsByDomainName("transipdemo.net") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Simple loop to print your addons | ||
// For more info about the email api, see: https://api.transip.nl/rest/docs.html#email | ||
fmt.Println(strings.Repeat("-", 50)) | ||
for _, addon := range addons { | ||
fmt.Printf("Addon ID %d with disk space: %d, additional mailboxes: '%d' \n", addon.ID, addon.DiskSpace, addon.Mailboxes) | ||
} | ||
fmt.Println(strings.Repeat("-", 50)) | ||
} |