Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 1b29fe4

Browse files
committed
[#1904] Support legacy schema in (*repo.Profile).GetModeratedFixedFee
1 parent b179c50 commit 1b29fe4

File tree

5 files changed

+174
-15
lines changed

5 files changed

+174
-15
lines changed

repo/profile.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,23 @@ func (p *Profile) GetVersion() uint32 {
7979
// GetModeratedFixedFee returns the fixed CurrencyValue for moderator services
8080
// currently set on the Profile
8181
func (p *Profile) GetModeratedFixedFee() (*CurrencyValue, error) {
82-
switch p.GetVersion() {
83-
default:
84-
if p.IsModerationEnabled() &&
85-
p.profileProto.ModeratorInfo.Fee != nil &&
86-
p.profileProto.ModeratorInfo.Fee.FixedFee != nil {
82+
if p.IsModerationEnabled() &&
83+
p.profileProto.ModeratorInfo.Fee != nil &&
84+
p.profileProto.ModeratorInfo.Fee.FixedFee != nil {
85+
switch p.GetVersion() {
86+
case 5:
8787
var (
8888
amt = p.profileProto.ModeratorInfo.Fee.FixedFee.BigAmount
8989
code = p.profileProto.ModeratorInfo.Fee.FixedFee.AmountCurrency
9090
)
9191
return NewCurrencyValueFromProtobuf(amt, code)
92+
default: // v4 and earlier
93+
var (
94+
amt = strconv.Itoa(int(p.profileProto.ModeratorInfo.Fee.FixedFee.Amount))
95+
code = p.profileProto.ModeratorInfo.Fee.FixedFee.CurrencyCode
96+
)
97+
return NewCurrencyValueWithLookup(amt, code)
98+
9299
}
93100
}
94101
return nil, fmt.Errorf("fixed fee not found")

repo/profile_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,43 @@ func TestProfileInvalidAsModeratorWithoutInfo(t *testing.T) {
493493
}
494494
}
495495

496+
func TestProfileGetModeratedFixedFee(t *testing.T) {
497+
var examples = []string{
498+
"v0-profile-moderator-fixed-fee",
499+
"v4-profile-moderator-fixed-fee",
500+
"v5-profile-moderator-fixed-fee",
501+
}
502+
503+
for _, e := range examples {
504+
var (
505+
fixtureBytes = factory.MustLoadProfileFixture(e)
506+
actualProfile, err = repo.UnmarshalJSONProfile(fixtureBytes)
507+
)
508+
if err != nil {
509+
t.Errorf("unmarshal (%s): %s", e, err)
510+
continue
511+
}
512+
513+
fee, err := actualProfile.GetModeratedFixedFee()
514+
if err != nil {
515+
t.Errorf("fixed fee (%s): %s", e, err)
516+
continue
517+
}
518+
519+
// all profile fixtures have equivalent data
520+
// validate they are intepreted from their resepctive schemas
521+
if fee.Amount.String() != "100" {
522+
t.Errorf("amount (%s): expected (%s), got (%s)", e, "100", fee.Amount.String())
523+
}
524+
if fee.Currency.Code.String() != "USD" {
525+
t.Errorf("currency code (%s): expected (%s), got (%s)", e, "USD", fee.Currency.Code.String())
526+
}
527+
if fee.Currency.Divisibility != 2 {
528+
t.Errorf("currency code (%s): expected (%d), got (%d)", e, 2, fee.Currency.Divisibility)
529+
}
530+
}
531+
}
532+
496533
func TestProfileSetModeratorFixedFee(t *testing.T) {
497534
var (
498535
p = factory.MustNewProfile()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"peerID": "QmQifVhzhnHRu9bGT9WNcbbuc5EF2bXRF6iJpSzNj7yRtQ",
3+
"handle": "",
4+
"name": "Moderator",
5+
"location": "",
6+
"about": "",
7+
"shortDescription": "2.3 (0.13.0) Moderator",
8+
"nsfw": false,
9+
"vendor": true,
10+
"moderator": true,
11+
"moderatorInfo": {
12+
"description": "I'm a test moderator. Please do not use me.",
13+
"termsAndConditions": "Test moderation only. Will resolve all disputes 50/50.",
14+
"languages": [
15+
"en-US"
16+
],
17+
"acceptedCurrencies": [
18+
"ZEC",
19+
"LTC",
20+
"BTC",
21+
"BCH"
22+
],
23+
"fee": {
24+
"fixedFee": {
25+
"currencyCode": "USD",
26+
"amount": 100
27+
},
28+
"percentage": 0,
29+
"feeType": "FIXED"
30+
}
31+
},
32+
"contactInfo": {
33+
"website": "",
34+
"email": "",
35+
"phoneNumber": "",
36+
"social": [
37+
]
38+
},
39+
"colors": {
40+
"primary": "#FFFFFF",
41+
"secondary": "#ECEEF2",
42+
"text": "#252525",
43+
"highlight": "#2BAD23",
44+
"highlightText": "#252525"
45+
},
46+
"avatarHashes": {
47+
"tiny": "zb2rhfLcAfDqyRudWcTxzZztdCNmRD2iEH2wt964QNQznZv8Y",
48+
"small": "zb2rhXQbxyCzYD7aZ9PVfAx7D5hdRdjZU6MctGS5fbN76BDwc",
49+
"medium": "zb2rhXowgKjLbEBEVGC9HPhxtuHKzjtzmEinmGWGm8nztnqjd",
50+
"large": "zb2rhoZUSE7eWk8Tb1RQhFfrapgZ2Zt5Xp4XhLJwaa7oondCZ",
51+
"original": "zb2rhmx8WX4nTL35kJGySgQuS1wzfsBbn9WvvArkxDTQW2Bqy"
52+
},
53+
"bitcoinPubkey": "02951689e5abda5d499607c15bb60d9c7a3b072dc7d4d1211f825334d20908b4f5",
54+
"lastModified": "2019-03-15T14:50:58.913432876Z",
55+
"currencies": [
56+
"ZEC",
57+
"LTC",
58+
"BTC",
59+
"BCH"
60+
]
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"peerID": "QmQifVhzhnHRu9bGT9WNcbbuc5EF2bXRF6iJpSzNj7yRtQ",
3+
"handle": "",
4+
"name": "Moderator",
5+
"location": "",
6+
"about": "",
7+
"shortDescription": "2.3 (0.13.0) Moderator",
8+
"nsfw": false,
9+
"vendor": true,
10+
"moderator": true,
11+
"moderatorInfo": {
12+
"description": "I'm a test moderator. Please do not use me.",
13+
"termsAndConditions": "Test moderation only. Will resolve all disputes 50/50.",
14+
"languages": [
15+
"en-US"
16+
],
17+
"acceptedCurrencies": [
18+
"ZEC",
19+
"LTC",
20+
"BTC",
21+
"BCH"
22+
],
23+
"fee": {
24+
"fixedFee": {
25+
"currencyCode": "USD",
26+
"amount": 100
27+
},
28+
"percentage": 0,
29+
"feeType": "FIXED"
30+
}
31+
},
32+
"contactInfo": {
33+
"website": "",
34+
"email": "",
35+
"phoneNumber": "",
36+
"social": [
37+
]
38+
},
39+
"colors": {
40+
"primary": "#FFFFFF",
41+
"secondary": "#ECEEF2",
42+
"text": "#252525",
43+
"highlight": "#2BAD23",
44+
"highlightText": "#252525"
45+
},
46+
"avatarHashes": {
47+
"tiny": "zb2rhfLcAfDqyRudWcTxzZztdCNmRD2iEH2wt964QNQznZv8Y",
48+
"small": "zb2rhXQbxyCzYD7aZ9PVfAx7D5hdRdjZU6MctGS5fbN76BDwc",
49+
"medium": "zb2rhXowgKjLbEBEVGC9HPhxtuHKzjtzmEinmGWGm8nztnqjd",
50+
"large": "zb2rhoZUSE7eWk8Tb1RQhFfrapgZ2Zt5Xp4XhLJwaa7oondCZ",
51+
"original": "zb2rhmx8WX4nTL35kJGySgQuS1wzfsBbn9WvvArkxDTQW2Bqy"
52+
},
53+
"bitcoinPubkey": "02951689e5abda5d499607c15bb60d9c7a3b072dc7d4d1211f825334d20908b4f5",
54+
"lastModified": "2019-03-15T14:50:58.913432876Z",
55+
"currencies": [
56+
"ZEC",
57+
"LTC",
58+
"BTC",
59+
"BCH"
60+
],
61+
"version": 4
62+
}

test/factory/listing.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package factory
22

33
import (
4-
"errors"
54
"fmt"
65
"io/ioutil"
7-
"os"
86
"path/filepath"
97

108
"github.com/OpenBazaar/openbazaar-go/pb"
@@ -15,14 +13,8 @@ import (
1513

1614
// MustLoadListingFixture - load listing json from fixtures
1715
func MustLoadListingFixture(fixtureName string) []byte {
18-
gopath := os.Getenv("GOPATH")
19-
repoPath := filepath.Join("src", "github.com", "OpenBazaar", "openbazaar-go")
20-
fixturePath, err := filepath.Abs(filepath.Join(gopath, repoPath, "test", "factory", "fixtures", "listings"))
21-
if err != nil {
22-
panic(errors.New("cannot create absolute path"))
23-
}
24-
filename := fmt.Sprintf("%s.json", fixtureName)
25-
b, err := ioutil.ReadFile(filepath.Join(fixturePath, filename))
16+
filename := filepath.Join(fixtureLoadPath(), "listings", fmt.Sprintf("%s.json", fixtureName))
17+
b, err := ioutil.ReadFile(filename)
2618
if err != nil {
2719
panic(fmt.Errorf("cannot find fixture (%s): %s", fixtureName, err))
2820
}

0 commit comments

Comments
 (0)