Repository: bxcodec/faker
Pull request #113 (2020-06-19 16:16:14)

added fake jwt-token support
Added jwt-token support in case someone wants to write tests on their 'Passport service' or something like it

codecov[bot]: # [Codecov](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=h1) Report
> Merging [#113](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=desc) into [master](https://codecov.io/gh/bxcodec/faker/commit/02fc1b24f66d9227a11c9bc375adb50d8cdbfd28&el=desc) will **decrease** coverage by `0.23%`.
> The diff coverage is `73.33%`.

[![Impacted file tree graph](https://codecov.io/gh/bxcodec/faker/pull/113/graphs/tree.svg?width=650&height=150&src=pr&token=cLPGB7MHVc)](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #113      +/-   ##
==========================================
- Coverage   93.87%   93.63%   -0.24%     
==========================================
  Files          11       11              
  Lines        1289     1304      +15     
==========================================
+ Hits         1210     1221      +11     
- Misses         43       45       +2     
- Partials       36       38       +2     
```


| [Impacted Files](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [faker.go](https://codecov.io/gh/bxcodec/faker/pull/113/diff?src=pr&el=tree#diff-ZmFrZXIuZ28=) | `91.25% <ø> (ø)` | |
| [internet.go](https://codecov.io/gh/bxcodec/faker/pull/113/diff?src=pr&el=tree#diff-aW50ZXJuZXQuZ28=) | `82.22% <73.33%> (-1.12%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=footer). Last update [02fc1b2...9187dee](https://codecov.io/gh/bxcodec/faker/pull/113?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/faker.go b/faker.go
index 0080cfe..3bdbd44 100644
--- a/faker.go
+++ b/faker.go
@@ -78,6 +78,7 @@ const (
 	IPV4Tag               = "ipv4"
 	IPV6Tag               = "ipv6"
 	PASSWORD              = "password"
+	JWT                   = "jwt"
 	LATITUDE              = "lat"
 	LONGITUDE             = "long"
 	CreditCardNumber      = "cc_number"
@@ -133,6 +134,7 @@ var defaultTag = map[string]string{
 	IPV4Tag:               IPV4Tag,
 	IPV6Tag:               IPV6Tag,
 	PASSWORD:              PASSWORD,
+	JWT:                   JWT,
 	CreditCardType:        CreditCardType,
 	CreditCardNumber:      CreditCardNumber,
 	LATITUDE:              LATITUDE,
@@ -182,6 +184,7 @@ var mapperTag = map[string]TaggedFunction{
 	IPV4Tag:               GetNetworker().IPv4,
 	IPV6Tag:               GetNetworker().IPv6,
 	PASSWORD:              GetNetworker().Password,
+	JWT:                   GetNetworker().Jwt,
 	CreditCardType:        GetPayment().CreditCardType,
 	CreditCardNumber:      GetPayment().CreditCardNumber,
 	LATITUDE:              GetAddress().Latitude,
diff --git a/internet.go b/internet.go
index 796a9bd..799bee9 100644
--- a/internet.go
+++ b/internet.go
@@ -51,6 +51,7 @@ type Networker interface {
 	IPv4(v reflect.Value) (interface{}, error)
 	IPv6(v reflect.Value) (interface{}, error)
 	Password(v reflect.Value) (interface{}, error)
+	Jwt(v reflect.Value) (interface{}, error)
 }
 
 // Internet struct
@@ -250,3 +251,29 @@ func Password() string {
 		return p
 	}).(string)
 }
+
+func (internet Internet) jwt() (string, error) {
+	element, err := randomString(40, &LangENG)
+	sl := element[:]
+	if err != nil {
+		return "", err
+	}
+	return strings.Join([]string{sl, sl, sl}, "."), nil
+}
+
+// Jwt returns a jwt-like random string in xxxx.yyyy.zzzz style
+func (internet Internet) Jwt(v reflect.Value) (interface{}, error) {
+	return internet.jwt()
+}
+
+// Jwt get jwt-like string
+func Jwt() string {
+	return singleFakeData(JWT, func() interface{} {
+		i := Internet{}
+		p, err := i.jwt()
+		if err != nil {
+			panic(err.Error())
+		}
+		return p
+	}).(string)
+}


Test patch:
diff --git a/example_with_tags_test.go b/example_with_tags_test.go
index 3fa4b29..f45ac20 100644
--- a/example_with_tags_test.go
+++ b/example_with_tags_test.go
@@ -17,6 +17,7 @@ type SomeStructWithTags struct {
 	IPV4               string  `faker:"ipv4"`
 	IPV6               string  `faker:"ipv6"`
 	Password           string  `faker:"password"`
+	Jwt                string  `faker:"jwt"`
 	PhoneNumber        string  `faker:"phone_number"`
 	MacAddress         string  `faker:"mac_address"`
 	URL                string  `faker:"url"`
@@ -85,6 +86,7 @@ func Example_withTags() {
 			IPV4: 99.23.42.63
 			IPV6: 975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d
 			Password: dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTPOmNyMFb
+			Jwt: HDMNSOKhEIYkPIuHcVjfCtHlKkaqLGrUEqjKVkgR.HDMNSOKhEIYkPIuHcVjfCtHlKkaqLGrUEqjKVkgR.HDMNSOKhEIYkPIuHcVjfCtHlKkaqLGrUEqjKVkgR
 			PhoneNumber: 792-153-4861
 			MacAddress: cd:65:e1:d4:76:c6
 			URL: https://www.oEuqqAY.org/QgqfOhd
diff --git a/faker_test.go b/faker_test.go
index ecefa25..8e297e6 100644
--- a/faker_test.go
+++ b/faker_test.go
@@ -204,6 +204,7 @@ type TaggedStruct struct {
 	IPV4               string  `faker:"ipv4"`
 	IPV6               string  `faker:"ipv6"`
 	Password           string  `faker:"password"`
+	Jwt                string  `faker:"jwt"`
 	PhoneNumber        string  `faker:"phone_number"`
 	MacAddress         string  `faker:"mac_address"`
 	URL                string  `faker:"url"`
@@ -249,6 +250,7 @@ func (t TaggedStruct) String() string {
 	IPV4: %s,
 	IPV6: %s,
 	Password: %s,
+	Jwt: %s,
 	PhoneNumber: %s,
 	MacAddress: %s,
 	URL: %s,
@@ -283,7 +285,7 @@ func (t TaggedStruct) String() string {
 	ID: %s,
 }`, t.Latitude, t.Longitude, t.CreditCardNumber,
 		t.CreditCardType, t.Email, t.DomainName, t.IPV4,
-		t.IPV6, t.Password, t.PhoneNumber, t.MacAddress,
+		t.IPV6, t.Password, t.Jwt, t.PhoneNumber, t.MacAddress,
 		t.URL, t.UserName, t.TollFreeNumber,
 		t.E164PhoneNumber, t.TitleMale, t.TitleFemale,
 		t.FirstName, t.FirstNameMale, t.FirstNameFemale, t.LastName,
diff --git a/internet_test.go b/internet_test.go
index db91dd6..e937303 100644
--- a/internet_test.go
+++ b/internet_test.go
@@ -2,6 +2,7 @@ package faker
 
 import (
 	"reflect"
+	"regexp"
 	"strings"
 	"testing"
 
@@ -157,3 +158,11 @@ func TestFakePassword(t *testing.T) {
 		t.Error("Expected hash password")
 	}
 }
+
+func TestFakeJWT(t *testing.T) {
+	jwt := Jwt()
+	reg := regexp.MustCompile(`[a-zA-Z]+.[a-zA-Z]+.[a-zA-Z]+`)
+	if !reg.MatchString(jwt) {
+		t.Error("Invalid format on JWT token")
+	}
+}