diff --git a/api/controller/contact.go b/api/controller/contact.go index 4a047ef03..41482cf94 100644 --- a/api/controller/contact.go +++ b/api/controller/contact.go @@ -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, @@ -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, @@ -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) } diff --git a/api/controller/contact_test.go b/api/controller/contact_test.go index 4f8e30ff5..06c0087da 100644 --- a/api/controller/contact_test.go +++ b/api/controller/contact_test.go @@ -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: "awesome_moira_user@gmail.com", @@ -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, @@ -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: "some@mail.com", + 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(), @@ -339,12 +363,14 @@ func TestUpdateContact(t *testing.T) { Convey("Success", func() { contactDTO := dto.Contact{ Value: "some@mail.com", + 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, } @@ -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() { diff --git a/api/dto/contact.go b/api/dto/contact.go index 12e9fd1da..84ffe2f57 100644 --- a/api/dto/contact.go +++ b/api/dto/contact.go @@ -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:"devops@example.com"` ID string `json:"id,omitempty" example:"1dd38765-c5be-418d-81fa-7a5f879c2315"` User string `json:"user,omitempty" example:""` diff --git a/api/handler/contact_test.go b/api/handler/contact_test.go index 8482ddcee..2801f9cef 100644 --- a/api/handler/contact_test.go +++ b/api/handler/contact_test.go @@ -199,6 +199,7 @@ func TestCreateNewContact(t *testing.T) { newContactDto := &dto.Contact{ ID: defaultContact, + Name: "Mail Alerts", Type: "mail", Value: "moira@skbkontur.ru", User: login, @@ -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, @@ -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) }) @@ -389,6 +392,7 @@ func TestUpdateContact(t *testing.T) { contactID := defaultContact updatedContactDto := &dto.Contact{ ID: contactID, + Name: "Mail Alerts", Type: "mail", Value: "moira@skbkontur.ru", User: defaultLogin, @@ -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, @@ -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, @@ -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, diff --git a/datatypes.go b/datatypes.go index e403f15db..34fe869da 100644 --- a/datatypes.go +++ b/datatypes.go @@ -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:"devops@example.com"` ID string `json:"id" example:"1dd38765-c5be-418d-81fa-7a5f879c2315"` User string `json:"user" example:""`