func TestGetSignupPackageInfoValid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") // Apid return free package fakeClient := clientfakes.NewFakeClient() expectedResponse := client.PackageRecord{ID: 1, IsFree: 1, PackageGroupID: 1, Credits: 12000} fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return []client.PackageRecord{expectedResponse}, nil }) adaptor := New(fakeClient) freePackage := adaptor.GetSignupPackageInfo("abc") assert.Equal(t, expectedResponse, freePackage) // Apid return paid package, then our function will return 12k Free package fakeClient = clientfakes.NewFakeClient() fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return []client.PackageRecord{client.PackageRecord{ID: 1, IsFree: 0, PackageGroupID: 1, Credits: 12000}}, nil }) adaptor = New(fakeClient) freePackage = adaptor.GetSignupPackageInfo("abc") assert.Equal(t, client.PackageRecord{ID: FreePackageID, IsFree: 1, PackageGroupID: FreePackageGroupID, Credits: FreeAccountCreditsLimits}, freePackage) }
func TestSetUserPackageError(t *testing.T) { userID := 180 packageID := 11 packageGroupID := 2 fakeClient := clientfakes.NewFakeClient() expectedPackage := Package{ID: packageID, GroupID: packageGroupID} fakeClient.RegisterFunction("getPackage", func(params url.Values) (interface{}, error) { return expectedPackage, nil }) fakeClient.RegisterFunction("delete", func(params url.Values) (interface{}, error) { return 1, nil }) fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { return "", errors.New("some apid error with add to user package") }) adaptor := New(fakeClient) adaptorErr := adaptor.SetUserPackage(userID, packageID) require.NotNil(t, adaptorErr, "expected error") }
func TestGetSubusers(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expected := []client.Subuser{ { ID: 1, Username: "******", Email: "*****@*****.**", }, { ID: 2, Username: "******", Email: "*****@*****.**", }, } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getSubusers", func(params url.Values) (interface{}, error) { return expected, nil }) adaptor := New(fakeClient) actual, adaptorError := adaptor.GetSubusers(&client.SubuserRequest{UserID: 180}) assert.Nil(t, adaptorError) if assert.NotNil(t, actual) { assert.Equal(t, len(expected), len(actual)) for i, _ := range actual { assert.Equal(t, expected[i].ID, actual[i].ID) assert.Equal(t, expected[i].Username, actual[i].Username) assert.Equal(t, expected[i].Email, actual[i].Email) } } }
func TestAddUserPartnerErrWithSerializeString(t *testing.T) { expectedGetResult := make([]PartnerRecord, 1) expectedGetResult[0] = PartnerRecord{ID: 1, Label: "partner name"} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) { return "success", nil }) fakeClient.RegisterFunction("get", func(param url.Values) (interface{}, error) { return expectedGetResult, nil }) adaptor := New(fakeClient) adaptorErr := adaptor.AddUserPartner( client.Signup{ Username: "******", Email: "*****@*****.**", Password: "******", // bad serialized array SendGridPartner: "czo0OiJhYWFhIjs=", }) assert.NotNil(t, adaptorErr, "an error should have occured") }
func TestEditUser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") userID := 23 var actualParams url.Values fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) { actualParams = params return 1, nil }) adaptor := New(fakeClient) request := &client.User{ ID: userID, AccountOwnerID: 123, Username: "******", Email: "*****@*****.**", } actual, adaptorErr := adaptor.EditUser(request) assert.NoError(t, adaptorErr) assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"*****@*****.**"}}) assert.True(t, actual) }
func TestGetUserByUsernameSlaveFailMasterSuccess(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") username := "******" bad := User{ID: 0} expected := User{ID: 123, Username: username, AccountOwnerID: 4000, AccountID: "iamanaccountid"} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserInfo", func(params url.Values) (interface{}, error) { return bad, nil }) fakeClient.RegisterFunction("getUserInfoMaster", func(params url.Values) (interface{}, error) { return expected, nil }) adaptor := New(fakeClient) actual, clientErr := adaptor.GetUserByUsername(username) assert.Nil(t, clientErr) require.NotNil(t, actual) assert.Equal(t, expected.ID, actual.ID) assert.Equal(t, expected.Username, actual.Username) assert.Equal(t, expected.AccountOwnerID, actual.AccountOwnerID) assert.Equal(t, expected.AccountID, actual.AccountID) }
func TestEditUserNoUpdate(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") // When a call is made to update a user, but there are no changes in the db // (If we are editing to the same info the user already has in the db) userID := 23 var actualParams url.Values fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) { actualParams = params return 0, nil }) adaptor := New(fakeClient) request := &client.User{ ID: userID, AccountOwnerID: 123, Username: "******", Email: "*****@*****.**", } actual, adaptorErr := adaptor.EditUser(request) assert.NoError(t, adaptorErr) assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"*****@*****.**"}}) assert.True(t, actual) }
func TestAddUserPackageForSubuser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() addPackageSpy := fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) { return "success", nil }) adaptor := New(fakeClient) err := adaptor.AddUserPackage( client.Signup{ UserID: 180, ResellerID: 888, UserPackageStatus: 444, }) assert.Len(t, err, 0) //verify subuser params are removed params := addPackageSpy.CalledParams["values"][0] exists := strings.Contains(params, "package_id") assert.False(t, exists, "package id should be null") exists = strings.Contains(params, "package_group_id") assert.False(t, exists, "package group id should be null") exists = strings.Contains(params, "package_status") assert.False(t, exists, "package status should be null") //verify subuser params is modified re := regexp.MustCompile(`{"status":"(\d*)"}`) matches := re.FindAllStringSubmatch(params, -1) status := matches[0][1] exists = strings.Contains(params, "status") assert.True(t, exists, "status is passed through") assert.Equal(t, "444", status, "status should be pass through") //default is 1 = SendGrid Free }
func TestGetUserHolds(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") var userIDFromParams int expected := UserHolds{ "10": "test", "101": "test2", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserHolds", func(params url.Values) (interface{}, error) { userIDFromParams, _ = strconv.Atoi(params.Get("userid")) return expected, nil }) adaptor := New(fakeClient) actual, err := adaptor.GetUserHolds(23) require.Nil(t, err) require.NotNil(t, actual) assert.Equal(t, actual, expected) assert.Equal(t, userIDFromParams, 23) }
func TestAddFiltersInvalid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") user := client.Signup{ Username: "******", UserID: 1, Password: "******", Email: "*****@*****.**", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) { return 1, nil }) expectedApidError := fmt.Errorf("apid internal server error") fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) { return 0, expectedApidError }) adaptor := New(fakeClient) errs := adaptor.AddFilters(user) assert.Len(t, errs, 4) assert.Equal(t, errs[0], expectedApidError) assert.Equal(t, errs[1], expectedApidError) assert.Equal(t, errs[2], expectedApidError) assert.Equal(t, errs[3], expectedApidError) }
func TestEditUserProfileError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") ln.SetOutput(ioutil.Discard, "test_logger") someProfile := client.UserProfile{ UserID: 23, Phone: "123-123-1234", Website: "www.google.com", FirstName: "Homer", LastName: "Simpson", Address1: "12345 Seasame St.", City: "Springfield", State: "IL", Zip: "11252", Country: "USA", Company: "Simpsons", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUserProfile", func(params url.Values) (interface{}, error) { return 0, errors.New("some serious error happened") }) adaptor := New(fakeClient) success, err := adaptor.EditUserProfile(&someProfile) require.NotNil(t, err) assert.False(t, success) }
func TestUpdateURLMailDomainFailure(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() var actualParams url.Values err := errors.New("error") fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { actualParams = params return nil, err }) adaptor := New(fakeClient) testUserID := 1 testUrlDomain := fmt.Sprintf("u%d.ct.sendgrid.net", testUserID) testMailDomain := "sendgrid.net" updateErr := adaptor.UpdateURLMailDomain(testUserID) assert.NotNil(t, updateErr) assert.Equal(t, actualParams.Get("tableName"), "user") assert.Equal(t, actualParams.Get("where"), fmt.Sprintf(`{"id" : "%d"}`, testUserID)) assert.Equal(t, actualParams.Get("values"), fmt.Sprintf(`[{"url_domain": "%s"},{"mail_domain": "%s"}]`, testUrlDomain, testMailDomain)) }
func TestAddFiltersValid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") user := client.Signup{ Username: "******", UserID: 1, Password: "******", Email: "*****@*****.**", } // filter already enabled fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) { return 1, nil }) // no errors, but register one row updated fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) { return 1, nil }) adaptor := New(fakeClient) errs := adaptor.AddFilters(user) assert.Len(t, errs, 0) }
func TestCreateUserError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expectedNewUserID := 1234 // value to be passed into apid expectedUsername := "******" // expected output from apidadaptor expectedErr := adaptor.NewError("error when adding user") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) { username := params.Get("username") if username == expectedUsername { return 0, fmt.Errorf("some apid error") } return expectedNewUserID, nil }) adaptor := New(fakeClient) _, adaptorErr := adaptor.CreateUser( client.Signup{ Username: expectedUsername, Email: "*****@*****.**", Password: "******", }) if assert.NotNil(t, adaptorErr, adaptorErr.Error()) { assert.Equal(t, expectedErr.Error(), adaptorErr.Error()) assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode) } }
func TestCreateUserPasswordValidationError(t *testing.T) { expectedNewUserID := 1234 // value to be passed into apid expectedUsername := "******" // expected output from apidadaptor (pass along password errors) expectedApidErr := "your password sucks bro. because reasons." expectedErr := adaptor.NewErrorWithStatus("password invalid - "+expectedApidErr, http.StatusBadRequest) fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) { username := params.Get("username") if username == expectedUsername { // apid returns a json hash with the key exists on duplicate entries return 0, fmt.Errorf(expectedApidErr) } return expectedNewUserID, nil }) adaptor := New(fakeClient) _, adaptorErr := adaptor.CreateUser( client.Signup{ Username: expectedUsername, Email: "*****@*****.**", Password: "******", }) if assert.NotNil(t, adaptorErr, adaptorErr.Error()) { assert.Equal(t, expectedErr.Error(), adaptorErr.Error()) assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode) } }
func TestAddIPGroup(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") missingUserIDError := errors.New("missing userid") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addUserIpGroup", func(param url.Values) (interface{}, error) { if param.Get("userid") == "0" { return 0, missingUserIDError } return 1, nil }) adaptor := New(fakeClient) // test good case err := adaptor.AddIPGroup( client.Signup{ UserID: 180, }) assert.Len(t, err, 0) // test missing user error err = adaptor.AddIPGroup(client.Signup{}) assert.Len(t, err, 1) assert.Equal(t, missingUserIDError, err[0]) }
func TestGetEmptyUser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") userID := 23 expected := User{ID: userID} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserInfo", func(params url.Values) (interface{}, error) { userIDFromParams, _ := strconv.Atoi(params.Get("userid")) switch userIDFromParams { case userID: return expected, nil } return nil, fmt.Errorf("not a valid call") }) adaptor := New(fakeClient) actual, adaptorErr := adaptor.GetUser(userID) require.Nil(t, adaptorErr) require.NotNil(t, actual) assert.Equal(t, expected.ID, actual.ID) assert.Equal(t, expected.Username, actual.Username) assert.Equal(t, expected.AccountOwnerID, actual.AccountOwnerID) assert.Equal(t, expected.AccountID, actual.AccountID) }
func TestAddUserPartner(t *testing.T) { expectedGetResult := make([]PartnerRecord, 1) expectedGetResult[0] = PartnerRecord{ID: 1, Label: "partner name"} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) { return "success", nil }) fakeClient.RegisterFunction("get", func(param url.Values) (interface{}, error) { return expectedGetResult, nil }) adaptor := New(fakeClient) adaptorErr := adaptor.AddUserPartner( client.Signup{ Username: "******", Email: "*****@*****.**", Password: "******", // Partner credential is string SendGridPartner: "YTozOntzOjc6InBhcnRuZXIiO3M6MTA6InNlbmR3aXRodXMiO3M6NDoiaGFzaCI7czozOiJhYmMiO3M6MTg6InBhcnRuZXJfY3JlZGVudGlhbCI7czo4OiIxYTJiM2M0ZCI7fQ==", }) assert.Nil(t, adaptorErr, fmt.Sprintf("no error should have occured, got %s", adaptorErr.Error())) adaptorErr = adaptor.AddUserPartner( client.Signup{ Username: "******", Email: "*****@*****.**", Password: "******", // Partner credential is int SendGridPartner: "YTozOntzOjc6InBhcnRuZXIiO3M6MTA6InNlbmR3aXRodXMiO3M6NDoiaGFzaCI7czozOiJhYmMiO3M6MTg6InBhcnRuZXJfY3JlZGVudGlhbCI7aToxMDAwMDA7fQ==", }) assert.Nil(t, adaptorErr, fmt.Sprintf("no error should have occured, got %s", adaptorErr.Error())) }
func TestSetUserPackageSuccess(t *testing.T) { userID := 180 packageID := 11 packageGroupID := 2 addMethodCalled := false fakeClient := clientfakes.NewFakeClient() expectedPackage := Package{ID: packageID, GroupID: packageGroupID} fakeClient.RegisterFunction("getPackage", func(params url.Values) (interface{}, error) { return expectedPackage, nil }) fakeClient.RegisterFunction("delete", func(params url.Values) (interface{}, error) { return 1, nil }) fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { addMethodCalled = true assert.True(t, strings.Contains(params.Get("values"), strconv.Itoa(packageGroupID)), "the package group uuid was looked up and stored") return "success", nil }) adaptor := New(fakeClient) adaptorErr := adaptor.SetUserPackage(userID, packageID) require.Nil(t, adaptorErr, "expected no error, got "+adaptorErr.Error()) assert.True(t, addMethodCalled, "expect add method called in apid because tests are run in that fake") }
func TestInsertDeactivationReaonsApidErr(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() expectedValidReasons := []userChurnReason{ userChurnReason{ID: 1, Reason: "reason_a"}, userChurnReason{ID: 2, Reason: "reason_b"}, } fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { return []competitor{competitor{ID: 3}}, nil } return expectedValidReasons, nil }) fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { return "success", nil } return "", errors.New("some apid error adding to user_churn") }) adaptor := New(fakeClient) reason := "reason_a" moving := true inHouse := true adaptorErr := adaptor.InsertDeactivationReason(1800, reason, moving, inHouse, "new provider", "notes on why") require.NotNil(t, adaptorErr, "should error") }
func TestSetCreditLimits(t *testing.T) { fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("setUserCreditLimit", func(params url.Values) (interface{}, error) { return 1, nil }) adaptor := New(fakeClient) success, err := adaptor.SetCreditLimits(180, 1000, "monthly") assert.Nil(t, err) assert.Equal(t, success, 1) }
func TestDeleteCreditLimits(t *testing.T) { fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("removeUserCreditLimit", func(params url.Values) (interface{}, error) { return 1, nil }) adaptor := New(fakeClient) success, err := adaptor.DeleteCreditLimits(180) assert.Nil(t, err) assert.Equal(t, success, 1) }
func TestEditUserProfile(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expected := client.UserProfile{ UserID: 23, Phone: "123-123-1234", Website: "www.google.com", FirstName: "Homer", LastName: "Simpson", Address1: "12345 Seasame St.", City: "Springfield", State: "IL", Zip: "11252", Country: "USA", Company: "Simpsons", } toModify := client.UserProfile{} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUserProfile", func(params url.Values) (interface{}, error) { userID, _ := strconv.Atoi(params.Get("user_id")) toModify.UserID = userID toModify.Phone = params.Get("phone") toModify.Website = params.Get("website") toModify.FirstName = params.Get("first_name") toModify.LastName = params.Get("last_name") toModify.Address1 = params.Get("address") toModify.City = params.Get("city") toModify.State = params.Get("state") toModify.Zip = params.Get("zip") toModify.Country = params.Get("country") toModify.Company = params.Get("company") return 1, nil }) adaptor := New(fakeClient) success, err := adaptor.EditUserProfile(&expected) require.Nil(t, err) assert.True(t, success) assert.Equal(t, expected.Phone, toModify.Phone) assert.Equal(t, expected.Website, toModify.Website) assert.Equal(t, expected.FirstName, toModify.FirstName) assert.Equal(t, expected.LastName, toModify.LastName) assert.Equal(t, expected.Address1, toModify.Address1) assert.Equal(t, expected.City, toModify.City) assert.Equal(t, expected.State, toModify.State) assert.Equal(t, expected.Zip, toModify.Zip) assert.Equal(t, expected.Country, toModify.Country) assert.Equal(t, expected.Company, toModify.Company) }
func TestAddTalonFingerprintSuccessOnDuplicate(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { return "", errors.New("key exists") }) adaptor := New(fakeClient) ok, adaptorErr := adaptor.AddUserFingerprint(TalonFingerprint{}) require.Nil(t, adaptorErr, "no error should have occured") assert.True(t, ok, "fingerprint added") }
func TestAddTalonFingerprintSuccessError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { return "", errors.New("some error") }) adaptor := New(fakeClient) ok, adaptorErr := adaptor.AddUserFingerprint(TalonFingerprint{}) assert.NotNil(t, adaptorErr, "error should have occured") assert.False(t, ok, "fingerprint errored") }
func TestAddCreditLimitsFailure(t *testing.T) { fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("setUserCreditLimit", func(params url.Values) (interface{}, error) { return 0, errors.New("something went wrong!") }) adaptor := New(fakeClient) err := adaptor.AddCreditLimits( client.Signup{ UserID: 180, }) assert.Len(t, err, 1, "apid generated an expected error") }
func TestUpdateUserBISSomeAPIDErr(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "", errors.New("some apid err") }) adaptor := New(fakeClient) err := adaptor.UpdateUserBI(client.Provision{}) require.NotNil(t, err, "should err") }
func TestGetSignupPackageInfoInvalid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return nil, fmt.Errorf("apid internal server error") }) adaptor := New(fakeClient) freePackage := adaptor.GetSignupPackageInfo("abc") assert.Equal(t, client.PackageRecord{ID: FreePackageID, IsFree: 1, PackageGroupID: FreePackageGroupID, Credits: FreeAccountCreditsLimits}, freePackage) }
func TestDeactivateUserPackageSuccess(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "success", nil }) adaptor := New(fakeClient) adaptorErr := adaptor.DeactivateUserPackage(1800) require.Nil(t, adaptorErr, fmt.Sprintf("got %#v, but should not error", adaptorErr.Error())) }
func TestDeactivateUserPackageError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "", errors.New("some apid error for downgrade user package") }) adaptor := New(fakeClient) adaptorErr := adaptor.DeactivateUserPackage(1800) require.NotNil(t, adaptorErr, "should error") }