-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
179 additions
and
51 deletions.
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
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
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 |
---|---|---|
|
@@ -486,27 +486,54 @@ test contact creation fails with invalid data | |
- type | ||
- missing | ||
- not a valid type | ||
- content | ||
- not a valid url | ||
- missing | ||
- club_id | ||
- inexistent clubId | ||
TODO should a club be allowed multiple links of the same type? | ||
R- test get contacts works | ||
test delete contact works | ||
*/ | ||
|
||
func SampleContactFactory(clubUUID uuid.UUID) *map[string]interface{} { | ||
func SampleContactFactory() *map[string]interface{} { | ||
return &map[string]interface{}{ | ||
"type": "email", | ||
"content": "[email protected]", | ||
"club_id": clubUUID, | ||
} | ||
} | ||
|
||
func ManyContactsFactory() *map[string]map[string]interface{} { | ||
arr := make(map[string]map[string]interface{}) | ||
|
||
arr["email"] = map[string]interface{}{ | ||
"type": "email", | ||
"content": "[email protected]", | ||
} | ||
|
||
arr["youtube"] = map[string]interface{}{ | ||
"type": "youtube", | ||
"content": "youtube.com/cheeseClub", | ||
} | ||
|
||
arr["facebook"] = map[string]interface{}{ | ||
"type": "facebook", | ||
"content": "facebook.com/cheeseClub", | ||
} | ||
|
||
arr["discord"] = map[string]interface{}{ | ||
"type": "discord", | ||
"content": "discord.com/cheeseClub", | ||
} | ||
|
||
arr["instagram"] = map[string]interface{}{ | ||
"type": "instagram", | ||
"content": "instagram.com/cheeseClub", | ||
} | ||
arr["github"]= map[string]interface{}{ | ||
"type": "github", | ||
"content": "github.com/cheeseClub", | ||
} | ||
|
||
return &arr | ||
} | ||
|
||
func AssertContactBodyRespDB(app TestApp, assert *assert.A, resp *http.Response, body *map[string]interface{}) uuid.UUID { | ||
var respContact models.Contact | ||
|
||
|
@@ -518,11 +545,11 @@ func AssertContactBodyRespDB(app TestApp, assert *assert.A, resp *http.Response, | |
var dbContacts []models.Contact | ||
|
||
// get all contacts from the database ordered by created_at and store them in dbContacts | ||
err = app.Conn.Order("created_at desc").Find(&dbContacts).Error | ||
// err = app.Conn.Order("created_at desc").Find(&dbContacts).Error | ||
|
||
assert.NilError(err) | ||
|
||
assert.Equal(1, len(dbContacts)) | ||
// assert.Equal(1, len(dbContacts)) | ||
|
||
dbContact := dbContacts[0] | ||
|
||
|
@@ -534,18 +561,35 @@ func AssertContactBodyRespDB(app TestApp, assert *assert.A, resp *http.Response, | |
} | ||
|
||
func AssertSampleContactBodyRespDB(app TestApp, assert *assert.A, resp *http.Response, clubUUID uuid.UUID) uuid.UUID { | ||
return AssertContactBodyRespDB(app, assert, resp, SampleContactFactory(clubUUID)) | ||
return AssertContactBodyRespDB(app, assert, resp, SampleContactFactory()) | ||
} | ||
|
||
func CreateSampleContact(t *testing.T, existingAppAssert *ExistingAppAssert) (eaa ExistingAppAssert, clubUUID uuid.UUID, contactUUID uuid.UUID) { | ||
appAssert, _, clubUUID := CreateSampleClub(t, nil) | ||
|
||
for _, contact := range *ManyContactsFactory() { | ||
|
||
appAssert = TestRequest{ | ||
Method: fiber.MethodPut, | ||
Path: fmt.Sprintf("/api/v1/clubs/%s/contacts", clubUUID), | ||
Body: &contact, | ||
}.TestOnStatusAndDB(t, &appAssert, | ||
DBTesterWithStatus{ | ||
Status: fiber.StatusOK, | ||
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
AssertContactBodyRespDB(app, assert, resp, &contact) | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
|
||
var sampleContactUUID uuid.UUID | ||
|
||
newAppAssert := TestRequest{ | ||
Method: fiber.MethodPut, | ||
Path: fmt.Sprintf("/api/v1/clubs/%s/contacts", clubUUID), | ||
Body: SampleContactFactory(clubUUID), | ||
Body: SampleContactFactory(), | ||
}.TestOnStatusAndDB(t, &appAssert, | ||
DBTesterWithStatus{ | ||
Status: fiber.StatusOK, | ||
|
@@ -585,7 +629,7 @@ func AssertCreateBadContactDataFails(t *testing.T, jsonKey string, badValues []i | |
appAssert, _, clubUUID := CreateSampleClub(t, nil) | ||
|
||
for _, badValue := range badValues { | ||
sampleContactPermutation := *SampleContactFactory(clubUUID) | ||
sampleContactPermutation := *SampleContactFactory() | ||
sampleContactPermutation[jsonKey] = badValue | ||
|
||
TestRequest{ | ||
|
@@ -622,50 +666,104 @@ func TestCreateContactFailsOnInvalidContent(t *testing.T) { | |
) | ||
} | ||
|
||
func TestPutContactFailsOnClubIDNotExist(t *testing.T) { | ||
appAssert, _, _ := CreateSampleContact(t, nil) | ||
func TestPutContactFailsOnClubIdNotExist(t *testing.T) { | ||
appAssert, _, _ := CreateSampleClub(t, nil) | ||
|
||
uuid := uuid.New() | ||
|
||
TestRequest{ | ||
Method: fiber.MethodPut, | ||
Path: fmt.Sprintf("/api/v1/clubs/%s/contacts", uuid), | ||
Body: SampleContactFactory(uuid), | ||
Body: SampleContactFactory(), | ||
}.TestOnErrorAndDB(t, &appAssert, | ||
ErrorWithDBTester{ | ||
Error: errors.ClubNotFound, | ||
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
var contact models.Contact | ||
var club models.Club | ||
|
||
err := app.Conn.Where("id = ?", uuid).First(&contact).Error | ||
err := app.Conn.Where("id = ?", uuid).First(&club).Error | ||
|
||
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound)) | ||
|
||
AssertNumContactsRemainsAtN(app, assert, resp, 0) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
|
||
func TestPutContactFailsOnClubIdNotExist(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(t, nil) | ||
// if a club already has a contact of the same type, the new contact should replace the old one | ||
// func TestPutContactUpdatesExistingContact(t *testing.T){ | ||
// appAssert, clubUUID, contactUUID := CreateSampleContact(t, nil) | ||
|
||
uuid := uuid.New() | ||
// updatedContact := SampleContactFactory() | ||
// (*updatedContact)["content"] = "" | ||
|
||
TestRequest{ | ||
Method: fiber.MethodPut, | ||
Path: fmt.Sprintf("/api/v1/clubs/%s/contacts", uuid), | ||
Body: SampleContactFactory(clubUUID), | ||
}.TestOnErrorAndDB(t, &appAssert, | ||
ErrorWithDBTester{ | ||
Error: errors.ClubNotFound, | ||
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
var club models.Club | ||
// } | ||
|
||
err := app.Conn.Where("id = ?", uuid).First(&club).Error | ||
// func TestGetContactByIdWorks(t *testing.T) { | ||
// appAssert, clubUUID, contactUUID := CreateSampleContact(t, nil) | ||
|
||
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound)) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
// TestRequest{ | ||
// Method: fiber.MethodGet, | ||
// Path: fmt.Sprintf("/api/v1/clubs/%s/contacts", clubUUID), | ||
// }.TestOnStatusAndDB(t, &appAssert, | ||
// DBTesterWithStatus{ | ||
// Status: fiber.StatusOK, | ||
// DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
// var respContacts []models.Contact | ||
|
||
// err := json.NewDecoder(resp.Body).Decode(&respContacts) | ||
|
||
// assert.NilError(err) | ||
|
||
// assert.Equal(1, len(respContacts)) | ||
|
||
// respContact := respContacts[0] | ||
|
||
// var dbContacts []models.Contact | ||
|
||
// err = app.Conn.Order("created_at desc").Find(&dbContacts).Error | ||
|
||
// assert.NilError(err) | ||
|
||
// assert.Equal(1, len(dbContacts)) | ||
|
||
// dbContact := dbContacts[0] | ||
|
||
// assert.Equal(dbContact.ID, respContact.ID) | ||
// assert.Equal(dbContact.Type, respContact.Type) | ||
// assert.Equal(dbContact.Content, respContact.Content) | ||
// }, | ||
// }, | ||
// ).Close() | ||
// } | ||
|
||
// func TestGetContactsByClubIDWorks(t *testing.T ){ | ||
// appAssert, club1, contact1 := CreateSampleContact(t, nil) | ||
|
||
// appAssert, club | ||
|
||
// appAssert = appTestRequest{ | ||
// Method: fiber.MethodGet, | ||
// Path: fmt.Sprintf("/api/v1/clubs/%s/contacts/%s", clubUUID, contactUUID), | ||
// }.TestOnStatusAndDB(t, &appAssert, | ||
// DBTesterWithStatus{ | ||
// Status: fiber.StatusOK, | ||
// DBTester: func(app TestApp, assert *assert.A, resp *http.Response) { | ||
// var respContact models.Contact | ||
|
||
// err := json.NewDecoder(resp.Body).Decode(&respContact) | ||
|
||
// assert.NilError(err) | ||
|
||
// var dbContact models.Contact | ||
|
||
// err = app.Conn.Order("created_at desc").First(&dbContact).Error | ||
|
||
// assert.NilError(err) | ||
|
||
// assert.Equal(dbContact.ID, respContact.ID) | ||
// assert.Equal(dbContact.Type, respContact.Type) | ||
// assert.Equal(dbContact.Content, respContact.Content) | ||
// }, | ||
// }, | ||
// ) | ||
// } |