Skip to content

Commit

Permalink
feat(api): add custom name to contacts (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored May 21, 2024
1 parent ace8d26 commit 0311b07
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/controller/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func GetContactById(database moira.Database, contactID string) (*dto.Contact, *a

contactToReturn := &dto.Contact{
ID: contact.ID,
Name: contact.Name,
User: contact.User,
TeamID: contact.Team,
Type: contact.Type,
Expand All @@ -58,6 +59,7 @@ func CreateContact(dataBase moira.Database, auth *api.Authorization, contact *dt

contactData := moira.ContactData{
ID: contact.ID,
Name: contact.Name,
User: contact.User,
Team: teamID,
Type: contact.Type,
Expand Down Expand Up @@ -92,6 +94,7 @@ func CreateContact(dataBase moira.Database, auth *api.Authorization, contact *dt
func UpdateContact(dataBase moira.Database, contactDTO dto.Contact, contactData moira.ContactData) (dto.Contact, *api.ErrorResponse) {
contactData.Type = contactDTO.Type
contactData.Value = contactDTO.Value
contactData.Name = contactDTO.Name
if err := dataBase.SaveContact(&contactData); err != nil {
return contactDTO, api.ErrorInternalServer(err)
}
Expand Down
27 changes: 27 additions & 0 deletions api/controller/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestGetContactById(t *testing.T) {
Convey("Get contact by id should be success", t, func() {
contact := moira.ContactData{
ID: uuid.Must(uuid.NewV4()).String(),
Name: "awesome_name",
Type: "slack",
User: "awesome_moira_user",
Value: "[email protected]",
Expand All @@ -79,6 +80,7 @@ func TestGetContactById(t *testing.T) {
ShouldResemble,
&dto.Contact{
ID: contact.ID,
Name: contact.Name,
Type: contact.Type,
User: contact.User,
Value: contact.Value,
Expand Down Expand Up @@ -217,6 +219,28 @@ func TestCreateContact(t *testing.T) {
So(contact.ID, ShouldResemble, contact.ID)
})

Convey("Success with custom name", func() {
contact := dto.Contact{
ID: uuid.Must(uuid.NewV4()).String(),
Value: "[email protected]",
Type: "mail",
Name: "some-name",
}
expectedContact := moira.ContactData{
ID: contact.ID,
Value: contact.Value,
Type: contact.Type,
Name: contact.Name,
Team: teamID,
}
dataBase.EXPECT().GetContact(contact.ID).Return(moira.ContactData{}, database.ErrNil)
dataBase.EXPECT().SaveContact(&expectedContact).Return(nil)
err := CreateContact(dataBase, auth, &contact, "", teamID)
So(err, ShouldBeNil)
So(contact.TeamID, ShouldResemble, teamID)
So(contact.Name, ShouldResemble, expectedContact.Name)
})

Convey("Contact exists by id", func() {
contact := &dto.Contact{
ID: uuid.Must(uuid.NewV4()).String(),
Expand Down Expand Up @@ -339,12 +363,14 @@ func TestUpdateContact(t *testing.T) {
Convey("Success", func() {
contactDTO := dto.Contact{
Value: "[email protected]",
Name: "some-name",
Type: "mail",
}
contactID := uuid.Must(uuid.NewV4()).String()
contact := moira.ContactData{
Value: contactDTO.Value,
Type: contactDTO.Type,
Name: contactDTO.Name,
ID: contactID,
User: userLogin,
}
Expand All @@ -353,6 +379,7 @@ func TestUpdateContact(t *testing.T) {
So(err, ShouldBeNil)
So(expectedContact.User, ShouldResemble, userLogin)
So(expectedContact.ID, ShouldResemble, contactID)
So(expectedContact.Name, ShouldResemble, contactDTO.Name)
})

Convey("Error save", func() {
Expand Down
1 change: 1 addition & 0 deletions api/dto/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (*ContactList) Render(w http.ResponseWriter, r *http.Request) error {

type Contact struct {
Type string `json:"type" example:"mail"`
Name string `json:"name,omitempty" example:"Mail Alerts"`
Value string `json:"value" example:"[email protected]"`
ID string `json:"id,omitempty" example:"1dd38765-c5be-418d-81fa-7a5f879c2315"`
User string `json:"user,omitempty" example:""`
Expand Down
7 changes: 7 additions & 0 deletions api/handler/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func TestCreateNewContact(t *testing.T) {

newContactDto := &dto.Contact{
ID: defaultContact,
Name: "Mail Alerts",
Type: "mail",
Value: "[email protected]",
User: login,
Expand All @@ -212,6 +213,7 @@ func TestCreateNewContact(t *testing.T) {
mockDb.EXPECT().GetContact(defaultContact).Return(moira.ContactData{}, db.ErrNil).Times(1)
mockDb.EXPECT().SaveContact(&moira.ContactData{
ID: newContactDto.ID,
Name: newContactDto.Name,
Type: newContactDto.Type,
Value: newContactDto.Value,
User: newContactDto.User,
Expand Down Expand Up @@ -271,6 +273,7 @@ func TestCreateNewContact(t *testing.T) {
So(actual.Type, ShouldEqual, newContactDto.Type)
So(actual.User, ShouldEqual, newContactDto.User)
So(actual.Value, ShouldEqual, newContactDto.Value)
So(actual.Name, ShouldEqual, newContactDto.Name)
So(response.StatusCode, ShouldEqual, http.StatusOK)
})

Expand Down Expand Up @@ -389,6 +392,7 @@ func TestUpdateContact(t *testing.T) {
contactID := defaultContact
updatedContactDto := &dto.Contact{
ID: contactID,
Name: "Mail Alerts",
Type: "mail",
Value: "[email protected]",
User: defaultLogin,
Expand All @@ -401,6 +405,7 @@ func TestUpdateContact(t *testing.T) {

mockDb.EXPECT().SaveContact(&moira.ContactData{
ID: updatedContactDto.ID,
Name: updatedContactDto.Name,
Type: updatedContactDto.Type,
Value: updatedContactDto.Value,
User: updatedContactDto.User,
Expand All @@ -410,6 +415,7 @@ func TestUpdateContact(t *testing.T) {
testRequest := httptest.NewRequest(http.MethodPut, "/contact/"+contactID, bytes.NewBuffer(jsonContact))
testRequest = testRequest.WithContext(middleware.SetContextValueForTest(testRequest.Context(), ContactKey, moira.ContactData{
ID: contactID,
Name: updatedContactDto.Name,
Type: updatedContactDto.Type,
Value: updatedContactDto.Value,
User: updatedContactDto.User,
Expand Down Expand Up @@ -441,6 +447,7 @@ func TestUpdateContact(t *testing.T) {

mockDb.EXPECT().SaveContact(&moira.ContactData{
ID: updatedContactDto.ID,
Name: updatedContactDto.Name,
Type: updatedContactDto.Type,
Value: updatedContactDto.Value,
User: updatedContactDto.User,
Expand Down
1 change: 1 addition & 0 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type Team struct {
// ContactData represents contact object.
type ContactData struct {
Type string `json:"type" example:"mail"`
Name string `json:"name,omitempty" example:"Mail Alerts"`
Value string `json:"value" example:"[email protected]"`
ID string `json:"id" example:"1dd38765-c5be-418d-81fa-7a5f879c2315"`
User string `json:"user" example:""`
Expand Down

0 comments on commit 0311b07

Please sign in to comment.