github.com/silveraid/fabric-ca@v1.1.0-preview.0.20180127000700-71974f53ab08/lib/serverenroll_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package lib 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/hyperledger/fabric-ca/api" 24 "github.com/hyperledger/fabric-ca/util" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestStateUpdate(t *testing.T) { 29 cleanTestSlateSE(t) 30 defer cleanTestSlateSE(t) 31 32 var err error 33 srv := TestGetRootServer(t) 34 35 err = srv.Start() 36 assert.NoError(t, err, "Failed to start server") 37 38 client := getTestClient(rootPort) 39 _, err = client.Enroll(&api.EnrollmentRequest{ 40 Name: "admin", 41 Secret: "adminpw", 42 }) 43 assert.NoError(t, err, "Failed to enroll 'admin' user") 44 45 registry := srv.CA.DBAccessor() 46 userInfo, err := registry.GetUser("admin", nil) 47 assert.NoError(t, err, "Failed to get user 'admin' from database") 48 // User state should have gotten updated to 1 after a successful enrollment 49 if userInfo.(*DBUser).State != 1 { 50 t.Error("Incorrect state set for user") 51 } 52 53 // Send bad CSR to cause the enroll to fail but the login to succeed 54 reqNet := &api.EnrollmentRequestNet{} 55 reqNet.SignRequest.Request = "badcsr" 56 body, err := util.Marshal(reqNet, "SignRequest") 57 assert.NoError(t, err, "Failed to marshal enroll request") 58 59 // Send the CSR to the fabric-ca server with basic auth header 60 post, err := client.newPost("enroll", body) 61 assert.NoError(t, err, "Failed to create post request") 62 post.SetBasicAuth("admin", "adminpw") 63 err = client.SendReq(post, nil) 64 if assert.Error(t, err, "Should have failed due to bad csr") { 65 assert.Contains(t, err.Error(), "CSR Decode failed") 66 } 67 68 // State should not have gotten updated because the enrollment failed 69 userInfo, err = registry.GetUser("admin", nil) 70 assert.NoError(t, err, "Failed to get user 'admin' from database") 71 if userInfo.(*DBUser).State != 1 { 72 t.Error("Incorrect state set for user") 73 } 74 75 err = srv.Stop() 76 assert.NoError(t, err, "Failed to stop server") 77 78 } 79 80 func cleanTestSlateSE(t *testing.T) { 81 err := os.RemoveAll(rootDir) 82 if err != nil { 83 t.Errorf("RemoveAll failed: %s", err) 84 } 85 err = os.RemoveAll("../testdata/msp") 86 if err != nil { 87 t.Errorf("RemoveAll failed: %s", err) 88 } 89 }